单目重载运算符时,由于要改变操作数自身的值,应采用引用参数传递
class point
{
private:
int num;
public:
point()
{
num = 0;
}
point(int n)
{
num = n;
}
point operator +(point mn);
point operator ++();
friend point operator -(point m, point n);
friend point operator --(point &m);
int get_num();
};
int point::get_num()
{
return num;
}
point point::operator +(point mn)
{
point item;
item = num + mn.num;
return item;
}
point point::operator ++()
{
++num;
return *this;
}
point operator -(point m, point n)
{
point temp;
temp.num = m.num + n.num;
return temp;
}
point operator --(point &m)
{
--m.num;
return m;
}