//单目运算符重载
#include <iostream>
using namespace std;
class Complex
{
public:
void display()
{
cout<<"x:"<<x<<" y:"<<y<<endl;
}
Complex(int x,int y)
{
this->x = x;
this->y = y;
}
Complex& operator+=(const Complex &another)
{
this->x += another.x;
this->y += another.y;
return *this;
}
Complex& operator++()
{
this->x++;
this->y++;
return *this;
}
private:
int x;
int y;
};
int main(void)
{
Complex c1(1,2);
c1.operator++();
c1.display();
return 0;
}
/*
因为单目 运算符 仅作用于当前对象,因此,当将这些运算符重载为函数成员时,就不再需要参数。
*/
01-20
10-06
3万+
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交