| |

VerySource

 Forgot password?
 Register
Search
Author: 格里弗斯

Quotes and continuous assignments, thank you!

[Copy link]

2

Threads

54

Posts

34.00

Credits

Newbie

Rank: 1

Credits
34.00

 Invalid IP Address

Post time: 2020-1-5 08:21:01
| Show all posts
For the form a = b = c, both the return value and the reference can be
For the form (a = b) = c, of course, returning an object is not possible, from another perspective
It can also be explained that because the return value creates a temporary object, and the temporary object is in
C ++ is const. It is not possible to assign to a const quantity.
A detailed explanation is given in "effective C ++" item15, comprehensively used in various situations, for = no other choice,
Returns a reference,
For individual cases, you can use some forms to return objects and the like. Step by step analysis, you can draw conclusions.
Reply

Use magic Report

2

Threads

54

Posts

34.00

Credits

Newbie

Rank: 1

Credits
34.00

 China

Post time: 2020-1-5 08:39:01
| Show all posts
(a = b) = c
====
In this form, if the return is not a reference, the value of c will not be assigned to either a or b.
If a reference is returned, the value of b is assigned to a, and then the value of c is assigned to a, overwriting the former.
If the object is returned, it cannot be executed first. If it can be executed, then the value of c is assigned to the created temporary object (note the const amount). At this time, it will not be assigned to a, and will not be assigned to b.
Reply

Use magic Report

2

Threads

54

Posts

34.00

Credits

Newbie

Rank: 1

Credits
34.00

 China

Post time: 2020-1-5 08:45:02
| Show all posts
I think it is clear, waiting for the master to summarize and 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 09:06:01
| Show all posts
But for the case of a = b = c, the return result is incorrect.
Reply

Use magic Report

2

Threads

54

Posts

34.00

Credits

Newbie

Rank: 1

Credits
34.00

 China

Post time: 2020-1-5 10:00:01
| Show all posts
#include <iostream>
using namespace std;

class A
{
public:
A (int i): a (i) {}
A operator = (const A&ra)
{
a = ra.a;
return * this;
}
void display ()
{
cout << a;
}
private:
int a;
};

int main ()
{
A a (1);
A b (2);
A c (3);

a = b = c;
a.display (); // 3
b.display (); // 3
c.display (); // 3
return 0;
}
Reply

Use magic Report

2

Threads

54

Posts

34.00

Credits

Newbie

Rank: 1

Credits
34.00

 China

Post time: 2020-1-5 10:06:01
| Show all posts
The simple form of a = b = c is wrong. What's the problem with 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 10:21:01
| Show all posts
Take a look at the link I posted. In the case of returning a value, the values ​​of a and b are both 99. Why is this? I also tried that program. Thank you.
Reply

Use magic Report

2

Threads

54

Posts

34.00

Credits

Newbie

Rank: 1

Credits
34.00

 China

Post time: 2020-1-5 10:27:01
| Show all posts
int main ()
{
A a (1);
A b (2);
A c (3);

a = b = c;
a.display (); // 3
b.display (); // 3
c.display (); // 3

A d (4);
A e (5);
A f (6);

(d = e) = f;
d.display ();
e.display ();
f.display ();
return 0;
}
Correct a statement. According to the test (d = e), the return value does not seem to be a const temporary object. Theoretically, it should be. Is this a compiler related issue?
Reply

Use magic Report

2

Threads

54

Posts

34.00

Credits

Newbie

Rank: 1

Credits
34.00

 China

Post time: 2020-1-5 11:03:01
| Show all posts
According to the situation of this test (d = e), the return value does not seem to be a const temporary object. In theory, it should be. Is this something related to the compiler, waiting for expert advice?
Found a gap in understanding, well, wait for the master
Reply

Use magic Report

2

Threads

54

Posts

34.00

Credits

Newbie

Rank: 1

Credits
34.00

 China

Post time: 2020-1-5 11:06:01
| Show all posts
up
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