0
What does this do?
class point { public: point(int,int); int &x(int);//what happens here? int &y(int);// why it is declared here? private: int _x;int _y;}
2 Answers
0
Maybe x and y are to function as getter and setters for the class objects _x and _y respectively. The int& means that these functions return a reference instead.
So, the prototype would have a definition like this:
int& x(int v=0)
//Method to get & set x using a function.
{ _x=v; return _x; }
Now this x can be used like this:
point p;
p.x(5); // Sets _x to 5.
p.x() = 2; // Sets _x to 2, we get a ref. to _x.
cout << p.x(); // Prints 2, as _x is returned.
0
thank you