#include<iostream> usingnamespace std; classPoint { public: Point(int X , int Y ); friend Point operator++(Point& p); friend Point operator++(Point& p, int); voidprint(void); private: int x; int y; };
Point::Point(int X=0, int Y=0) :x(X), y(Y) {} Point operator++(Point& p) { ++p.x; ++p.y; return p; } Point operator++(Point& p, int) { Point P(p); //先将形参的值保存下 p.x++; p.y++; return P; //返回被保存的形参的值 } voidPoint::print(){ cout << "(" << x << "," << y << ")" << endl; } intmain(){ Point a(1, 2); a++; a.print(); Point b; b = ++a; b.print(); a.print(); return0; }