简体   繁体   中英

Overloading copy assignment operator in c++

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.

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 (ie *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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM