由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - C++: define a reference always reference the same object
相关主题
相关话题的讨论汇总
话题: reference话题: object话题: fred话题: const话题: always
进入Programming版参与讨论
1 (共1页)
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
7
reference不能被重新赋值你不知道么?
1 (共1页)
进入Programming版参与讨论
相关主题
相关话题的讨论汇总
话题: reference话题: object话题: fred话题: const话题: always