重载 operator+=
和 operator-=
时是否需要自赋值检查,如下例?
例子:
class A
{
A operator+=(const A& a)
{
if(this != &a)
{
// operations
}
return *this;
}
}
最佳答案
Is a self assignment check, as exampled below, required when overloading the operaators += and -= in C++?
不,不是。事实上,这样做似乎是错误的。
使用它是完全合法且语义上有效的:
int i = 10;
i += i;
此处的期望是 i
将在该操作结束时设置为 20
。
关于C++:重载operator+=和operator-=时是否需要自赋值检查?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35961292/