Why we use reference return in assignment operator overloading and not at plus-minus ops?
Asked Answered
P

6

24

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):

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?

Perk answered 31/1, 2014 at 16:42 Comment(2)
If you returned a reference in a + or - binary operation, what should it refer to? It makes no sense to return a reference. The operation must create a new object.Knick
You don't want to be able to code a + b = c;Twelvemo
N
25

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.

Nisan answered 31/1, 2014 at 16:45 Comment(3)
Why? No, assignment chaining had noting to do with what is returned from assignment operator. Assignment chaining (as in your example) is possible in any version. Returning a const reference will prevent multiple modifications to lhs, as in '(a = b) = c', but that's a different story.Pomposity
In other words, situations that require non-const reference returned from assignment are rather esoteric and generally belong to "bad programming practice" category for which reason one might even see some sources to explicitly recommend returning const reference from assignment.Pomposity
@AndreyT: You're right, I was confusing myself. (Of course, you have to return something to allow chaining; but it could be a value or constant reference.)Nisan
C
10

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= is different because it's actually assigning something to this object.

operator+= and operator-= would act on this object, and are a closer analog to operator=.

Cartelize answered 31/1, 2014 at 16:46 Comment(0)
K
2

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. Thus you would modify one of a or b to be the sum of a and 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. The semantics you are suggesting are already offered by += and -=.

Karnak answered 31/1, 2014 at 16:49 Comment(0)
F
0

The link shown below has better explanation I guess return value of operator overloading in C++

Fleabite answered 4/5, 2015 at 14:48 Comment(0)
B
0

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 ;

here associativity is as followed: (a=(b=(c=3)))

but consider iostream operation cout << x << y << z ;

here associativity is as followed: (((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.

one more point , copy constructor will get called if you return by value. ( which is not the case with return by refrence)

Basicity answered 6/6, 2015 at 18:20 Comment(0)
B
0

Apart from efficiency during chained assignment (e.g., a = b = c;) as pointed out by the accepted answer, it is also a matter of convention. Please, refer Item 10 of Effective C++ by Scott Meyers. It is a convention followed by the built-in types and all the types in the standard library.

Blowzed answered 25/8, 2022 at 11:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.