As I\'ve understand, when overloading operator=, the return value should should be a non-const reference.
A& A::operator=( const A& )
{
// check for sel
If your assignment operator does not take a const reference parameter:
A& A::operator=(A&); // unusual, but std::auto_ptr does this for example.
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:
a = b = c;
The b = c
assignment would occur first, and return a copy (call it b'
) by value instead of returning a reference to b
. When the a = b'
assignment is done, the mutating assignment operator would change the b'
copy instead of the real b
.
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 if f()
mutates the object, it is not mutating a temporary.