单目运算符重载格式
++a和a++通过形参确定
data1 operator++()
{
++this->a;
return *this;
}
data1 operator++(int)
{
data1 temp=*this;
this->a++;
return temp;
}
举例使用单目运算符重载
#include "iostream"
using namespace std;
class data1
{
public :
int a;
data1(int a):a(a)
{
}
data1 operator++()
{
++this->a;
return *this;
}
data1 operator++(int)
{
data1 temp=*this;
this->a++;
return temp;
}
};
class data2
{
public :
int a;
data2(int a):a(a)
{
}
};
int main()
{
data1 a(20);
data1 b=a++;
cout << b.a << endl;
}