I often see such an example,
For a class and three objects a, b, c, if operator = returns a reference, a = b = c can succeed, but if it does not return a reference, an error will occur, who can tell me why this is so, thank you .
a = b = c;
Passing quotes:
High first efficiency avoids copy overhead
The second is to execute b = c, and its return value is used as the parameter of a =, so the return value type and parameter type of operator = are the same.
Passing objects can but increase the cost of copying,
Returns void No, interrupts the syntax form of continuous assignment
a = b = c; the processing order is a = (b = c); because the return value of b = c is a void, then the formal parameter of a = operator = needs const Type&, and the actual parameter is a void. Error
What I understand is that if it is not returning a reference, b.operator = (c) should be calculated first. At this time, an intermediate variable will be returned, and then this variable will be used as the parameter of a =. And b = didn't seem to succeed.
There is an execution result in the link I gave, and what I don't understand is why the values of a and b are 99, which means that b = did not succeed, but the value of b gave a. Thank you.
(A = B) = C If a value is returned instead of a reference, then A = B will get an rvalue, and an rvalue as an lvalue will certainly make a mistake.
Simply put a = 1, b = 2, c = 3 Now (a = b) = c is equivalent to 2 = c. Isn't it ridiculous to assign C to the value of 2? But if it returns a reference, it becomes (a) = c.
Tojulines:
a = b = c Shouldn't a = (b = c) be bracketed?
In the case of returning a reference, if (a = b) = c, the first step is a = b, then a = c, then what is the assignment to b?