| |

VerySource

 Forgot password?
 Register
Search
View: 3286|Reply: 23

Quotes and continuous assignments, thank you!

[Copy link]

1

Threads

8

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-1-4 23:00:01
| Show all posts |Read mode
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 .
Reply

Use magic Report

1

Threads

8

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-1-5 00:54:01
| Show all posts
Specific code can refer to http://bjtuwei.bokee.com/4223564.html
Reply

Use magic Report

2

Threads

54

Posts

34.00

Credits

Newbie

Rank: 1

Credits
34.00

 China

Post time: 2020-1-5 01:12:01
| Show all posts
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.
Reply

Use magic Report

2

Threads

54

Posts

34.00

Credits

Newbie

Rank: 1

Credits
34.00

 Spain

Post time: 2020-1-5 01:51:01
| Show all posts
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
Reply

Use magic Report

2

Threads

54

Posts

34.00

Credits

Newbie

Rank: 1

Credits
34.00

 China

Post time: 2020-1-5 02:06:01
| Show all posts
Personal opinions, experts advise
Reply

Use magic Report

1

Threads

8

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-1-5 03:51:01
| Show all posts
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.
Reply

Use magic Report

1

Threads

8

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-1-5 05:54:01
| Show all posts
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.
Reply

Use magic Report

2

Threads

54

Posts

34.00

Credits

Newbie

Rank: 1

Credits
34.00

 United States

Post time: 2020-1-5 06:09:01
| Show all posts
The returned object may not be efficient
Reply

Use magic Report

0

Threads

37

Posts

28.00

Credits

Newbie

Rank: 1

Credits
28.00

 China

Post time: 2020-1-5 07:03:01
| Show all posts
(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.
Reply

Use magic Report

1

Threads

8

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-1-5 07:45:01
| Show all posts
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?
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list