class LongInt
{
public: LongInt& operator++(); //returning a reference
}
LongInt LongInt::operator++()
{
...
LongInt d;
d.setVector(temp);
return d; //returning a reference to an object that will be wiped out because it is out of scope
}
我在这里有点困惑。我显然必须通过引用返回来重载增量运算符,但我认为这甚至是不可能的。我遇到内存错误,因为我的指针指向的对象消失了。那么我应该如何重载++ 运算符呢?
最佳答案
修改对象本身,然后返回*this
。
关于c++ - 重载必须返回对象引用的运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22185724/