自减运算符重载

#include<iostream>
using namespace std;
class point
{
friend ostream& operator<<(ostream& cout, point p);
private:
int x;
public:
point() {
x = 2;
}
point(int a)
{
x = a;
}
point operator--(int)
{
point t;
t=*this;
x--;
return t;

}
point& operator--()
{
x--;
return *this;
}
};
ostream& operator<<(ostream& cout, point p)
{
cout << p.x << endl;
return cout;
}

int main()
{
point m;
cout << --m << endl;
cout << m-- << endl;
cout << m << endl;
}

posted @ 2023-05-08 22:32  平安喜乐×  阅读(15)  评论(0)    收藏  举报