我成功地完全重载了一元++,--
后缀/前缀运算符,并且我的代码工作正常,但是在使用(++obj)++
语句时它会返回意外结果
这是代码
class ABC
{
public:
ABC(int k)
{
i = k;
}
ABC operator++(int )
{
return ABC(i++);
}
ABC operator++()
{
return ABC(++i);
}
int getInt()
{
return i;
}
private:
int i;
};
int main()
{
ABC obj(5);
cout<< obj.getInt() <<endl; //this will print 5
obj++;
cout<< obj.getInt() <<endl; //this will print 6 - success
++obj;
cout<< obj.getInt() <<endl; //this will print 7 - success
cout<< (++obj)++.getInt() <<endl; //this will print 8 - success
cout<< obj.getInt() <<endl; //this will print 8 - fail (should print 9)
return 1;
}
有什么解决办法或理由???