由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - [c++] reference 真得不能bound to a second object 么?
相关主题
C++: define a reference always reference the same object问个c++问题
how do I reseat a reference?a question about const reference
为啥允许这样的const设计问一个关于C++指针的问题
a simple C++ question请教一个const pointer的问题
问一道brainbench上的问题谁给解释一下这个c question
C++ Q05: pointer to constant variable问题
int F::*x = &F::x是什么意思?C++ Function Pointer Array 的问题
C++一问c++ interview question
相关话题的讨论汇总
话题: reference话题: object话题: c++话题: int话题: bound
进入Programming版参与讨论
1 (共1页)
G****A
发帖数: 4160
1
cited from C++ faq
"Unlike a pointer, once a reference is bound to an object, it can not be
reseated to another object.
....
In that sense, a reference is similar to a const pointer such as int*
const p."
我怎么觉得不对呢,还是我理解错了?
y*******g
发帖数: 6599
2
怎么不对?

【在 G****A 的大作中提到】
: cited from C++ faq
: "Unlike a pointer, once a reference is bound to an object, it can not be
: reseated to another object.
: ....
: In that sense, a reference is similar to a const pointer such as int*
: const p."
: 我怎么觉得不对呢,还是我理解错了?

G****A
发帖数: 4160
3
I tried (in g++) to bound a reference to a second object. It did not give
me any error.

【在 y*******g 的大作中提到】
: 怎么不对?
S**I
发帖数: 15689
4
Show the code.

【在 G****A 的大作中提到】
: I tried (in g++) to bound a reference to a second object. It did not give
: me any error.

A*********l
发帖数: 2005
5
reference is just another name of the object, so you can't re-bound it.
What you did is probably assign the second object to the original object
that the reference points to.

give

【在 G****A 的大作中提到】
: I tried (in g++) to bound a reference to a second object. It did not give
: me any error.

G****A
发帖数: 4160
6
That is the part confused me.
Since it mentioned in c++ FAQ "a reference is similar to a const pointer
such as int* const p.", so I thought there should be an error if I throw
following code to the compiler:
**************
int& c = a; //a, b are int variables.
int& c = b;
**************
I understand the point, but the expression in C++ FAQ here may not be
accurate.

【在 A*********l 的大作中提到】
: reference is just another name of the object, so you can't re-bound it.
: What you did is probably assign the second object to the original object
: that the reference points to.
:
: give

t****t
发帖数: 6806
7
就算挖坑也要有专业精神, 你这个显然是过不了编译的

【在 G****A 的大作中提到】
: That is the part confused me.
: Since it mentioned in c++ FAQ "a reference is similar to a const pointer
: such as int* const p.", so I thought there should be an error if I throw
: following code to the compiler:
: **************
: int& c = a; //a, b are int variables.
: int& c = b;
: **************
: I understand the point, but the expression in C++ FAQ here may not be
: accurate.

G****A
发帖数: 4160
8
什么时候大家都来这个版挖坑,买买提就牛打发了。:)
太急,打错了,应该是
**************
int& c = a; //a, b are int variables.
c = b;
**************

【在 t****t 的大作中提到】
: 就算挖坑也要有专业精神, 你这个显然是过不了编译的
X****r
发帖数: 3557
9

This is the same as a = b here. try:
printf("&a=%p, &b=%p, &c=%p\n", &a, &b, &c);

【在 G****A 的大作中提到】
: 什么时候大家都来这个版挖坑,买买提就牛打发了。:)
: 太急,打错了,应该是
: **************
: int& c = a; //a, b are int variables.
: c = b;
: **************

A*********l
发帖数: 2005
10
你这个就是重新给a赋值。
你没有办法让c成为另外一个int 的 reference,c永远只能是a的reference。

【在 G****A 的大作中提到】
: 什么时候大家都来这个版挖坑,买买提就牛打发了。:)
: 太急,打错了,应该是
: **************
: int& c = a; //a, b are int variables.
: c = b;
: **************

1 (共1页)
进入Programming版参与讨论
相关主题
c++ interview question问一道brainbench上的问题
最新某公司onsite面试题 (转载)C++ Q05: pointer to constant variable
c++ 弱问题:static const char* const 这两个const 分别是什么意思?int F::*x = &F::x是什么意思?
C++ pointer to function is buggyC++一问
C++: define a reference always reference the same object问个c++问题
how do I reseat a reference?a question about const reference
为啥允许这样的const设计问一个关于C++指针的问题
a simple C++ question请教一个const pointer的问题
相关话题的讨论汇总
话题: reference话题: object话题: c++话题: int话题: bound