简体   繁体   English

为什么我们在赋值运算符重载中使用引用返回而不是在加减运算中?

[英]Why we use reference return in assignment operator overloading and not at plus-minus ops?

As I read in books and in the web, in C++ we can overload the "plus" or "minus" operators with these prototypes (as member functions of a class Money ):正如我在书籍和 web 中所读到的,在 C++ 中,我们可以使用这些原型重载“加号”或“减号”运算符(作为class Money的成员函数):

const Money operator +(const Money& m2) const;

const Money operator -(const Money& m2) const;

and for the assignment operator with:对于赋值运算符:

const Money& operator =(const Money& m2);

Why use a reference to a Money object as a return value in the assignment operator overloading and not in the plus and minus operators?为什么在赋值运算符重载中而不是在加号和减号运算符中使用对 Money object 的引用作为返回值?

Returning a reference from assignment allows chaining: 从赋值返回引用允许链接:

a = b = c;  // shorter than the equivalent "b = c; a = b;"

(This would also work (in most cases) if the operator returned a copy of the new value, but that's generally less efficient.) (如果操作员返回了新值的副本,这也会起作用(在大多数情况下),但这通常效率较低。)

We can't return a reference from arithmetic operations, since they produce a new value. 我们不能从算术运算中返回引用,因为它们会产生新值。 The only (sensible) way to return a new value is to return it by value. 返回新值的唯一(明智的)方法是按值返回它。

Returning a constant value, as your example does, prevents move semantics, so don't do that. 正如您的示例所做的那样,返回一个常量值可以防止移动语义,所以不要这样做。

Because operator+ and operator- don't act on this object, but return a new object that is the summation (or subtraction) of this object from another. 因为operator+operator-对此对象起作用,而是返回一个新对象,该对象是该对象与另一个对象的求和(或减法)。

operator= is different because it's actually assigning something to this object. operator=是不同的,因为它实际上是为这个对象分配了一些东西。

operator+= and operator-= would act on this object, and are a closer analog to operator= . operator+=operator-=将作用于此对象,并且与operator=更接近。

Consider what you are asking. 考虑一下你在问什么。 You would want an expression, a + b , to return a reference to one of a or b, which would have the results of the expression. 您可能希望表达式a + b返回对a或b之一的引用,该引用将具有表达式的结果。 Thus you would modify one of a or b to be the sum of a and b. 因此,您可以将a或b中的一个修改为a和b的总和。 So you would want to redefine the semantics of the operator (+) to be the same as the operator (+=). 因此,您可能希望将运算符(+)的语义重新定义为与运算符(+ =)相同。 And like @manuell said, you would thus allow (a + b) = c . 就像@manuell说的那样,你会允许(a + b) = c The semantics you are suggesting are already offered by += and -=. 您建议的语义已经由+ =和 - =提供。

下面显示的链接有更好的解释我猜测C ++中运算符重载的返回值

I think its fine if you return by value in overloaded assignment operator , that is because of associativity of assignment operator. 我认为如果你在重载赋值运算符中按值返回它会很好,这是因为赋值运算符的相关性。 consider this: 考虑一下:

int a = b =c = 3 ; int a = b = c = 3;

here associativity is as followed: (a=(b=(c=3))) 这里的结合性如下:(a =(b =(c = 3)))

but consider iostream operation cout << x << y << z ; 但考虑iostream操作cout << x << y << z;

here associativity is as followed: (((cout << x )<< y) << z) ; 这里的结合性如下:(((cout << x)<< y)<< z);

you can see that x will be printed first , so if you return by value in overloading of << operator , return value will not be "lvalue" , while returning by refrence is a lvalue , so cascading of << operator can be achieve. 你可以看到x将首先被打印,所以如果你在<<运算符的重载中返回值,返回值将不是“左值”,而通过refrence返回是一个左值,所以可以实现级联的<<运算符。

one more point , copy constructor will get called if you return by value. 还有一点,如果按值返回,将调用复制构造函数。 ( which is not the case with return by refrence) (不是通过参考返回的情况)

Apart from efficiency during chained assignment (eg, a = b = c; ) as pointed out by the accepted answer, it is also a matter of convention.除了公认的答案指出的链式分配期间的效率(例如, a = b = c; )之外,这也是一个约定问题。 Please, refer Item 10 of Effective C++ by Scott Meyers.请参阅 Scott Meyers 的 Effective C++ 的第 10 项。 It is a convention followed by the built-in types and all the types in the standard library.它是内置类型和标准库中所有类型遵循的约定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么在重载+ =运算符时我们必须通过引用返回 - Why do we have to return by reference when overloading += operator “ return * this”赋值运算符重载 - “return *this” assignment operator overloading 为什么重载运算符<<必须通过引用返回? - why overloading of operator<< must return by reference? 为什么在重载 = 运算符时按引用返回? - Why return by reference while overloading = operator? 为什么我们必须从一元前缀运算符重载中返回const引用 - why do we have to return const reference from unary prefix operator overloading 重载赋值运算符:如果我们返回(* this),它是指针/地址处的值,那么赋值运算符的正确语法是什么? - Overloading assignment operator: if we return (*this), which is a value at a pointer/address, what's right syntax for assignment operator? 为什么赋值运算符应该返回对对象的引用? - Why should the assignment operator return a reference to the object? 无法从文件中读取“正负”符号 - Unable to read the “plus-minus” symbol from a file 为什么复制赋值运算符必须返回引用/ const引用? - Why must the copy assignment operator return a reference/const reference? 为什么对引用的赋值使用复制赋值运算符? - Why does assignment to a reference use the copy assignment operator?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM