i**p 发帖数: 902 | 1 How can I define a reference to ensure it will always reference the same
object but you can use the reference to modify the object's values?
The follow FAQ says a reference is always const. Do you agree it?
http://www.parashift.com/c++-faq-lite/const-correctness.html
...
[18.7] Does "Fred& const x" make any sense?
No, it is nonsense.
To find out what the above declaration means, you have to read it right-to-
left. Thus "Fred& const x" means "x is a const reference to a Fred". But
that is redunda |
i**p 发帖数: 902 | 2 I can reseat a reference by the following code and g++ does not generate any
error.
base& ref_1 = b;
ref_1 = c;
【在 i**p 的大作中提到】 : How can I define a reference to ensure it will always reference the same : object but you can use the reference to modify the object's values? : The follow FAQ says a reference is always const. Do you agree it? : http://www.parashift.com/c++-faq-lite/const-correctness.html : ... : [18.7] Does "Fred& const x" make any sense? : No, it is nonsense. : To find out what the above declaration means, you have to read it right-to- : left. Thus "Fred& const x" means "x is a const reference to a Fred". But : that is redunda
|
c*****g 发帖数: 119 | 3 it's not a reseat, but an assignment.
you can check what happens to b after you "reseating" ref_1 by c.
any
【在 i**p 的大作中提到】 : I can reseat a reference by the following code and g++ does not generate any : error. : base& ref_1 = b; : ref_1 = c;
|
i**p 发帖数: 902 | 4 You are right. b.i is 'C'. This is an assignment to b by reference ref_1.
【在 c*****g 的大作中提到】 : it's not a reseat, but an assignment. : you can check what happens to b after you "reseating" ref_1 by c. : : any
|
i**p 发帖数: 902 | 5 Now let us back to my original question.
How can I define a reference to ensure it will always reference the same
object but you can use the reference to modify the object's values?
So according to our analysis, we don't need to do anything special but just
declare a simple reference like this,
base& ref_1 = b;
Am I right?
【在 i**p 的大作中提到】 : You are right. b.i is 'C'. This is an assignment to b by reference ref_1.
|
f******y 发帖数: 2971 | 6 i think you are right. That is the definition of reference.
just
【在 i**p 的大作中提到】 : Now let us back to my original question. : How can I define a reference to ensure it will always reference the same : object but you can use the reference to modify the object's values? : So according to our analysis, we don't need to do anything special but just : declare a simple reference like this, : base& ref_1 = b; : Am I right?
|
W*********g 发帖数: 409 | |