如果我已经超载了+和 = 运算符,我是否必须重载 +=?
更一般地说,我应该重载哪些运算符?我知道重载的 +=
是一件好事,因为扩展到 a = a + b
会创建一个临时对象,而重载的 +=
> 可以避免这种情况。 未重载的 +=
能否有效地使用我的重载运算符?
More generally, what operators should I be overloading? I know that an overloaded +=
is a good thing because the expansion to a = a + b
creates a temporary object, whereas an overloaded +=
can avoid that. Will a +=
that is not overloaded effectively use my overloaded operators?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
实现operator+的最佳方法有利于代码重用,
然后
如果您必须复制右侧的副本(由于实现特定的原因,通常情况并非如此),您可以通过通过
operator+=
和operator+
的第二个参数中的值。The best way to implement
operator+
, favoring code reuse, isand then
If you have to do a copy of the right hand side (for implementation specific reasons, it is usually not the case), you can pass by value in both
operator+=
and the second argument ofoperator+
.您不必这样做。编译器不会抛出错误或警告,告诉您所做的事情是错误的。
然而,在绝大多数情况下你确实应该这样做。编译器不会将
a += b
转换为a = a + b
。You don't have to. The compiler won't throw an error or warning telling you that what you've done is wrong.
However, you really really should in the vast majority of cases. The compiler will NOT transform
a += b
intoa = a + b
.如果你不重载
+=
,它就不会被定义;你将无法使用它。我个人建议您提供它,因为如果
+
和=
可用,那么任何人都可能期望+=
可用。我还建议您使用 Boost.Operators为了使应该根据其他运算符自动生成的运算符重载。
If you don't overload
+=
it won't be defined; you won't be able to use it.I'd personally suggest you to provide it, since whoever would probably expect that
+=
is available, if+
and=
are.I'd also suggest you to use Boost.Operators in order to overload the operators that should be automatically generated in terms of others.
最好先实现
operator+=
和operator=
,因为根据它们实现operator+
很简单。 IIRC 这是 Scott Meyers 的 Effective C++ 版本。It is better to implement
operator+=
andoperator=
first, as implementingoperator+
in terms of them is trvial. IIRC this is in Effective C++ by Scott Meyers.是的,它是一个不同的运算符,因此您必须重载它。您可以(并且可能应该)在重载函数内使用其他运算符(例如“+”)。
Yes, it's a different operator, so you will have to overload it. You can (and probably should) use your other operators (e.g. '+') inside the overloaded function.
你必须重载+=。
然后,您可以重载它来执行完全不同的操作,但不建议这样做。 ;-)。
You have to overload +=.
Then, you could overload it to do something completely different, but it is not advised. ;-).
当你说“我必须……”时,只有当你需要使用它时。一般来说,最好先实现operator+=,然后再根据它实现operator+,因为大多数人都会弄错operator+(不要将其设为const,忘记返回副本...)
std::accumulate 使用 a = a + b如果你所做的只是编写一些东西来适应它,那么 += 本身不会有帮助,尽管你仍然可以根据它来实现运算符+。
当然,您可能决定不允许用户使用 += 修改您的对象(尽管您不介意他们制作副本)。
When you say "do I have to..", only if you need to use it. It is better generally to implement operator+= and then implement operator+ in terms of it, because most people get operator+ wrong (don't make it const, forget to return a copy...)
std::accumulate uses a = a + b and if all you are doing is writing something to fit it, += won't help on its own, although you can still then implement operator+ in terms of it.
Of course you might decide you don't want to allow users to modify your objects with += (whereas you don't mind them making copies).
不,你不必这样做。但不要认为因为你重载了+和=,你就免费拥有了+=。当然,你不会仅仅因为你已经超载了 - 并且>就认为这一点。你还有->操作员?
No you DO NOT HAVE TO. But just don't think that because you overloaded + and = you have += for free. Shurely you do not assume that just because you've overloaded - and > you also have -> operator?