As I've understand, when overloading operator=, the return value should should be a non-const reference.
A& A::operator=( const A& )
{
// check for self-assignment, do assignment
return *this;
}
It is non-const to allow non-const member functions to be called in cases like:
( a = b ).f();
But why should it return a reference? In what instance will it give a problem if the return value is not declared a reference, let's say return by value?
It's assumed that copy constructor is implemented correctly.
A good general advice when overloading operators is 'do as primitive types do', and the default behavior of assignment to a primitive type is that.
Not returning anything could be an option, to disable assignment inside other expressions if you feel the need, but returning a copy does not make sense at all: if the caller wants to make a copy they can make it out of the reference, if they do not need the copy there is no need to generate a temporary that is not needed.
Returning by reference reduces the time of performing chained operations. E. g. :
Let's see the actions which would be called, if
operator=
returns by value.c
makesc
equal tod
and then creates temporary anonymous object (calls copy ctor). Let's call ittc
.b
is called. Right hand side object is tc. Move assignment operator is called.b
becomes equal totc
. And then function copiesb
to temporary anonymous, let's call ittb
.a.operator=
returns temporary copy ofa
. After operator;
all three temporary objects are destroyedAltogether: 3 copy ctors, 2 move operators, 1 copy operator
Let's see what will change if operator= will return value by reference:
c
becomes equal tod
, reference to lvalue object is returnedb
becomes equal toc
, reference to lvalue object is returneda
becomes equal tob
, reference to lvalue object is returnedAltogether: only three copy operators is called and no ctors at all!
Moreover I recommend you to return value by const reference, it won't allow you to write tricky and unobvious code. With cleaner code finding bugs will be much easier :)
( a = b ).f();
is better to split to two linesa=b; a.f();
.P.S.: Copy assignment operator :
operator=(const Class& rhs)
.Move assignment operator :
operator=(Class&& rhs)
.This is Item 10 of Scott Meyers' excellent book, Effective C++. Returning a reference from
operator=
is only a convention, but it's a good one.If your assignment operator does not take a const reference parameter:
or if the class
A
has mutable members (reference count?), then it is possible that the assignment operator changes the object being assigned from as well as assigned to. Then if you had code like this:The
b = c
assignment would occur first, and return a copy (call itb'
) by value instead of returning a reference tob
. When thea = b'
assignment is done, the mutating assignment operator would change theb'
copy instead of the realb
.Another potential problem -- returning by value instead of by reference could cause slicing if you have virtual assignment operators. I'm not saying that's a good idea, but it could be a problem.
If you intend to do something like
(a = b).f()
then you will want it to return by reference so that iff()
mutates the object, it is not mutating a temporary.