Alright so I think I managed to get my head around the reference example I wrote this code just to fully understand the difference between using a reference and not using one.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
int main()
{
int a1 = 123;
int r1 = a1;
a1 = a1 + 5;
int a2 = 123;
int& r2 = a2;
a2 = a2 + 5;
std::cout << r1 << std::endl;
std::cout << r2 << std::endl;
}
| |
The inputs were as expexted r1 = 123 and r2 = 127
in order to get a pointer to an object you using the & operator. Note that this has nothing to do with the & symbol used for references. It's just the same symbol being reused for two different purposes. |
So there are two meaning to the & symbol one is for referencing as stated in the example above, I don't quite understand the second purpose, what is it called ?
I'm sure what you wrote @Peter87 makes sense to many other people, but I can't seem to get a grasp on pointers yet, I don't understand whats their purpose, I can give you a text book answer that I had to memorize for tests but I don't understand what it means.
Note that in your reference version of the Stroke function you should have used . instead of ->. |
So here both are acceptable but due to pointers being highly "unstable" we should always opt for the . when we have the choice to do so.
ptr->fun(); has the same meaning as (*ptr).fun(); |
This line right here, I feel that if I understand this I crack the enigma of pointers that is driving me absolutely crazy.
I'll attempt to explain from my own understanding now, pointers are used (*) when you want to point to another object meaning that
*p has to point to
a or b or c what have you, but cannot point to the number that directly represents a,b or c such as 123.
int* p = &a
I have no idea why in the hell we would put the second & meaning here this just makes everything a bit more confusing.
Returning to the other pointer
->, this is used to call a function similar to that of (.) and is used when ........... I don't know, damn. Someone please fill in the blanks.
Will you access different function if you put -> instead of (.) ?