下例中,如果前++函数返回值是引用就正确。
(1)如果是对象,在程序结束前,cc的值只增加了1。但是如果简单一个int j,(++j)++是不会错的。
(2)如果是对象,并且使用了copy构造函数,后++函数中的*this是前++返回值的copy,并且没有初始化。
对于这2种情况都不是很明白。
#include <iostream>
using namespace std;
class CC
{
public:
CC(int a);
//CC(CC & cc1); //(2)
CC & operator ++(); //(1)
const CC operator ++(int);
private:
int mm;
};
CC::CC(int a = 0)
{
mm = a;
}
CC & CC::operator ++() //(1)
{
mm++;
return * this;
}
const CC CC::operator ++(int)
{
CC temp = * this;
(this->mm)++;
return temp;
}
/*
CC::CC(CC & cc1) //(2)
{
cout << "Copy Constructor." << endl;
}
*/
int main()
{
CC cc;
(++cc)++;
return 0;
}