重载必须返回对对象的引用的运算符
Overloading an operator that must return a reference to an object
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++:类类型引用运算符=用法
- 为什么通过通用引用运算符 (&&) 将变量的引用传递给 Varadic 模板函数失败?
- 当取消引用运算符 (*) 重载时,*this 的使用是否受到影响?
- 为什么 C++ 中指向方法取消引用运算符的指针具有如此低的优先级
- 赋值不起作用,但带有取消引用运算符的地址起作用?
- 关于解引用运算符是否在表达式中产生对象对值的定义不明确
- 如何重载箭头取消引用运算符->() 不是针对实体对象,而是针对指针
- (取消)引用运算符的评估
- 重载取消引用运算符(运算符*())
- 如何重载取消引用运算符
- 结构引用和取消引用运算符
- 输入迭代器必须具有常量解引用运算符吗
- 对 std::atomic::load 的结果使用结构取消引用 (->) 运算符是否安全
- C++使用引用运算符传递数组
- 使用引用和取消引用运算符读取代码行时遇到问题
- 在C++向量中使用解引用运算符
- 是否可以根据基于的范围类型调用不同的取消引用运算符重载