obj1 *= obj2 VS. C++ 中的 obj1 = obj1 * obj2
假设我已经实现了 *=
、=
和 *
运算符重载,您更喜欢使用哪个复杂性? 谢谢。
Assuming that i've implemented *=
, =
and *
operators overloading, which will you prefer to use complexity wise?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
鉴于我通常会根据
operator *=
和副本来实现operator *
,因此没有理由更喜欢operator *
。operator *
(以及+
、-
、/
等)的正常实现通常应如下所示:Given that I’d usually implement
operator *
in terms ofoperator *=
and a copy, there is no reason ever to preferoperator *
.The normal implementation of
operator *
(and+
,-
,/
etc.) should usually look as follows:对于像您指定的那样的简单操作,这两种用法并不复杂。但是,对于较长的表达式,*= 可以使内容更具可读性。
Both usages are NOT complex for simple operations like the one you have specified. But, for longer expressions, *= can make things more readable.
如果您已经正常实现了它们,那么更喜欢
*=
。另一种方法需要制作一个副本,然后对其应用*=
,然后将其分配回来。If you've implemented them normally, then prefer
*=
. The other way will require making a copy, then applying*=
to it, then assigning it back.= 和 * 单独的实现更有用。它允许a*b*c*d类型的表达式。然而,正如本杰明·林德利 (Benjamin Lindley) 所指出的,请考虑创建额外副本的开销。
The = and * separate implementations are more useful. It allows a * b * c * d type expressions. However consider the overhead of additional copies being created, as noted by Benjamin Lindley.