[英]C++: Is self-assignment check required when overloading operator+= and operator-=?
Is a self assignment check, example below, required when overloading operator+=
and operator-=
? 重载
operator+=
和operator-=
是否需要进行以下示例的自我分配检查?
Example: 例:
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++?
在C ++中重载运算符+ =和-=时,是否需要进行自我分配检查(如下例所示)?
No, it is not. 不它不是。 In fact, it seems wrong to do so.
实际上,这样做似乎是错误的。
It is perfectly legal and semantically valid to use: 使用以下内容完全合法且在语义上有效:
int i = 10;
i += i;
The expectation here is that i
will be set to 20
at the end of that operation. 期望在该操作结束时将
i
设置为20
。
Is a self assignment check, as exampled below, required when overloading the operaators
+=
and-=
in C++?在C ++中重载运算符
+=
和-=
时,是否需要进行自我分配检查(如下例所示)?
No it's not required, it's even wrong. 不,这不是必需的,甚至是错误的。 The self check applies the wrong semantics here.
自检在此处应用了错误的语义。 Why shouldn't it be possible to add
A
to itself? 为什么不可以在自身上添加
A
?
Think about an example with a custom string class: 考虑一个带有自定义字符串类的示例:
MyString s = "abc";
s += s;
What would an innocent reader expect as the value of s
? 无辜的读者期望
s
的价值是什么? "abc"
or "abcabc"
? "abc"
还是"abcabc"
?
Also the operator should return a reference to the current instance: 此外,操作员还应返回对当前实例的引用:
A& operator+=(const A& a) {
// ^ ...
}
Since you're mucking with the internal state of the object, it might be necessary to do a "self check" in order to make sure to access the object consistently, but it isn't a self assignment check. 由于您正在处理对象的内部状态,因此可能有必要执行“自检”以确保一致地访问对象,但这不是自赋值检查。
a += a
certainly shouldn't result in a
being unchanged. a += a
当然不应该导致a
是不变的。
The self assignment test, eg if (this != &a)
is normally done for assignment operator operator=(const A& s)
for efficiency reasons or to avoid using destroyed data if the operator destroys some members before doing actual assignment. 自赋值测试,例如
if (this != &a)
出于效率方面的考虑,是否通常为赋值运算符operator=(const A& s)
完成if (this != &a)
或者如果运算符在执行实际赋值之前销毁了某些成员,则避免使用破坏的数据。 However, even there it is normally indicator of a design problem. 但是,即使在那儿也通常是设计问题的指示器。
As for operator+=
and operator-=
it is not required, and what is more, it would be a problem to use it! 至于
operator+=
和operator-=
则不是必需的,而且使用它会成问题! For eg integers, if x == 2
对于例如整数,如果
x == 2
x += x;
should change x
to 4. In your classes you should keep the same behaviour: a += a;
应将
x
更改为4。在您的班级中,您应保持相同的行为: a += a;
definitely should modify a
. 绝对应该修改
a
。
It depends entirely on what it means to "add" your objects. 这完全取决于“添加”对象的含义。 Since your question seems to be of a general nature (where addition may be any operation that can be combined with assignment), I can, off the top of my head, only think of an example with multiplication.
由于您的问题似乎具有一般性(加法可以是可以与赋值结合的任何运算),因此,我可以不去想一个带乘法的例子。
With bignums the multiplication process cannot be done in-place. 使用bignums时 ,乘法过程无法就地完成。 The destination object must be distinct from the source(s).
目标对象必须与源不同。 If not, the multiplication will fail.
否则,乘法将失败。
I admit that the example isn't perfect. 我承认这个例子并不完美。 Often, you can avoid asking the question about self-assignment by forcing the issue.
通常,您可以通过强制执行此问题来避免提出有关自我分配的问题。 A bignum multiply-assign might look like this:
一个bignum乘法分配可能看起来像这样:
bignum& operator += ( bignum& lhs, const bignum& rhs )
{
return lhs.swap( lhs + rhs ); // lhs = lhs + rhs
}
This leverages the existing operator+ to perform the multiplication to create a temporary result, which is then assigned intelligently using the bignum's swap()
member function (it has one, right?). 这将利用现有的operator +进行乘法运算以创建临时结果,然后使用bignum的
swap()
成员函数(它有一个,对吗?)进行智能分配。
Again, this was just an example off the top of my head... and not a perfect one. 再说一次,这只是我脑海中的一个例子……而不是一个完美的例子。 There might arise situations (and you know how your data is manipulated best) that require a different strategy.
可能会出现需要不同策略的情况(并且您知道如何最好地操纵数据)。
[edit] The good example of avoiding calling delete[]
on a resource was made above. [edit]上面避免了对资源调用
delete[]
]的好例子。 [/edit] [/编辑]
tl;dr tl; dr
If you need to check for self assignment, do it. 如果您需要检查自我分配,请执行此操作。 If you don't, then don't.
如果不这样做,那就不要。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.