0
votes

I had a doubt in the overloaded copy assignment operator. I read it in many books/websites that the signature for overloaded copy assignment operator looks like as follows:

Type &Type::operator=(const Type &rhs)

However, I dont understand why do we need to return it by reference, in fact doesnt it makes sense to return nothing? I mean when we return it by reference or by value, where is the return value being returned to since the other object has already being assigned in the overloaded copy asssignment operator For eg. If I have something like this in my main function:

int main(){
 Type a {<Some lieral value>}; //Initialise object a with some literal
 Type b {<Some literal value>}; 
 b=a; //Object b is already assigned when we call overloaded copy operator, which variable accepts the return value?
}

Note: The book/website says something about chain assignment, howver I dont understand where the value will be returned to when we have the above case.

1
Type b = a; doesn't invoke a copy assignment operator. It invokes a copy constructor.Igor Tandetnik
Returning a reference to *this allows chaining assignments, as in a = b = c; The thinking is, if it works for ints there is no reason it shouldn't work for your objects.Igor Tandetnik
Sorry @IgorTandetnik for the confusion. Edited the question. I meant when we use b = a, it will invoke the overloaded operator in which the operand on the left hand side has already got initialised with operand on the right. So why do we need to return any value in that case?Vikrant Waje
You don't, but there's no harm in returning a value even if it's not always used. If you add T c; c = b = a; then a return value from b=a will in fact be used.Igor Tandetnik
When you write your operator=, you wouldn't know whether, at some later time, someone might want to chain-assign your objects. It's perfectly valid to ignore a return value of any function; happens all the time.Igor Tandetnik

1 Answers

2
votes
Type b = a;

is not assignment. Is is a variable definition with initialization and causes the copy constructor of Type to be called with a as argument. Constructors don't have return values and as you correctly note it wouldn't make sense for Type b = a to have any result value, because it isn't even an expression.

However

b = a;

(with prior Type B;) is assignment. Assignment is an expression, so it has a result value that can be used in other expressions. For example the following is valid:

int a = 0;

(a = 2) += 1;

and will set a to 3. Here for fundamental types such as int, the assignment returns a reference to the left-hand side of the assignment.

With class types this is mimicked by having the copy assignment operator return a reference to the left-hand side of the assignment (i.e. *this) as well.

One typical use case for this is assigning to multiple instances:

Type a, b, c;
// Do something with c
a = b = c;

After the last line all three a, b and c will be in the same state, that of c prior to the change assignment, because the expression is parsed as a = (b = c); and the right-hand assignment returns a reference to b which is used to assign to a after c was assigned to b.

It is not required to return a reference to *this from an overloaded assignment operator. Doing is convention however, because it enables this particular use case. If you do not return a reference to *this users of your class may be surprised when that idiom suddenly doesn't work for your class, even if it may not be often used.

A value returned from a function (or operator) does not need to be used. If you write

b = a;

the returned reference is simply discarded. It is never stored anywhere and that is not a problem at all.