alright, im looking at a code here and the idea is difficult to understand.
#include <iostream>
using namespace std;
class Point
{
public :
int X,Y;
Point() : X(0), Y(0) {}
};
void MoveUp (Point * p)
{
p -> Y += 5;
}
int main()
{
Point point;
MoveUp(&point);
cout << point.X << point.Y;
return 0;
}
Alright, so i believe that a class is created and X and Y are declared and they are put inside a constructor
a method is created and the argument is Point * p, which means that we are going to stick the constructor’s pointer inside the function;
now we create an object called point then call our method and put the pointers address inside it?
isnt the pointers address just a memory number like 0x255255?
and why wasnt p ever declared?
(int * p = Y)
what is a memory addres exactly? that it can be used as an argument?
pwas declared.is a function that will take a pointer to a
Pointand add 5 to its Y value. It’s no different to the following:You wouldn’t say
nwasn’t defined in that case. It’s the same forpin your case.Perhaps some comments in the code would help:
Pointers are simply a level of indirection. In the code:
the effect of lines 3 and 4 are identical. That’s because
pXis a pointer toXso that*pX, the contents ofpX, is actuallyX.In your case,
p->Yis the same as(*p).Y, or theYmember of the class pointed to byp.