1

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.

7
  • 1
    Type b = a; doesn't invoke a copy assignment operator. It invokes a copy constructor. Commented Nov 17, 2019 at 1:10
  • 1
    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. Commented Nov 17, 2019 at 1:11
  • 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? Commented Nov 17, 2019 at 1:20
  • 1
    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. Commented Nov 17, 2019 at 1:24
  • 2
    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. Commented Nov 17, 2019 at 1:29

1 Answer 1

2
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.

Sign up to request clarification or add additional context in comments.

2 Comments

If I understand your reasoning correctly, for the example that you have provided (a=b=c), it would be simplified as follows: 1) a = (b = c); // copy assignment called first time in which b is assigned with c and reference is being returned simplifying the expression to 2) a = (reference returned when operator was first invoked) ; // Again the assignment oeprator will be called 3) But wait, where will the reference be returned to in that case since a is already being initialsed to the referenced variable(invoked in 1st statement)
@VikrantWaje The reference returned from b = c is passed as argument to the assignment operator call of the left-hand =. The return value of that operator call is discarded.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.