什么是“引用永远不能指向另一个对象”意思? [英] What does "reference can never be made to refer to another object" mean?
问题描述
我读了关于C ++引用变量和某处我发现这个:
一个const引用的声明是多余的,因为引用永远不能
这里,例如,我可以改变引用指向的对象:
void p(int& i){
int a = 7,c = 0;
i = a;
}
因此,参考永远不能指向另一个
解决方案这意味着,给定此代码
int a;
int b;
int& ref = a;
一旦你初始化了
ref
指向a
,没有办法让它引用b
或任何其他变量。
对引用所做的任何更改都将反映在用于初始化它的变量上。引用(它本身不是一个合适的对象)保持不变。
引用只有在它引用的对象保持活动时才可用。 p>
I read about C++ reference variables and somewhere I found this:
A declaration of a const reference is redundant since reference can never be made to refer to another object.
what does it mean?
Here, for example, I can change the object that the reference is referring to:
void p(int& i) { int a = 7,c = 0; i = a; }
So what does it mean that "Reference can never be made to refer to another object"?
thanks in advance.
解决方案It means, given this code
int a; int b; int& ref = a;
that, once you've initialized
ref
to refer toa
, there is no way you can make it refer tob
or any other variable.Any change you make to the reference will reflect on the variable that you used to initialize it with. The reference (which itself is not even a proper object) stays intact.
Also, a reference is only usable as long as the object it refers to stays alive.
这篇关于什么是“引用永远不能指向另一个对象”意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!