64,642
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
using namespace std;
class Integer
{
public:
Integer(int i): a(i) {}
Integer& operator++(); //前缀返回对象引用
Integer operator++(int); //后缀返回对象
~Integer();
protected:
private:
int a;
};
//前缀--返回左值
Integer& Integer::operator++()
{
cout << "Integer::operator() called." << endl;
a++;
return *this;
}
//注意:后缀需要加int
Integer Integer::operator++(int)
{
cout << "Integer::operator(int) called." << endl;
Integer temp = *this;
++(*this);
return temp; //返回this对象的旧值
}
int main()
{
Integer x = 1;
++x;
x++;
return 0;
}
Integer Integer::operator++(int)
{
cout << "Integer::operator(int) called." << endl;
Integer temp = *this;
//++(*this); //改成下面的
++a;
return temp; //返回this对象的旧值
}
~Integer(){} // 这样吧!