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;}

21st Feb 2018, 9:12 AM
Hema Chander
Hema Chander - avatar
2 ответов
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.
21st Feb 2018, 9:24 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
0
thank you
21st Feb 2018, 9:57 AM
Hema Chander
Hema Chander - avatar