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; : **************
|