什么是恒定参考? (不是对常量的引用)
为什么常量引用的行为方式与常量指针不同,以便我可以实际更改它们指向的对象?它们确实看起来像是另一个简单的变量声明。我为什么要使用它们?
这是我运行的一个简短示例,它编译并运行时没有错误:
int main (){
int i=0;
int y=1;
int&const icr=i;
icr=y; // Can change the object it is pointing to so it's not like a const pointer...
icr=99; // Can assign another value but the value is not assigned to y...
int x=9;
icr=x;
cout<<"icr: "<<icr<<", y:"<<y<<endl;
}
Why do constant references not behave the same way as constant pointers, so that I can actually change the object they are pointing to? They really seem like another plain variable declaration. Why would I ever use them?
This is a short example that I run which compiles and runs with no errors:
int main (){
int i=0;
int y=1;
int&const icr=i;
icr=y; // Can change the object it is pointing to so it's not like a const pointer...
icr=99; // Can assign another value but the value is not assigned to y...
int x=9;
icr=x;
cout<<"icr: "<<icr<<", y:"<<y<<endl;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
最明确的答案。
是否“X&” const x”有什么意义吗?
The clearest answer.
Does “X& const x” make any sense?
语句
icr=y;
不会使引用指向y
;它将y
的值分配给icr
引用的变量i
。引用本质上是 const,也就是说您无法更改它们所引用的内容。有一些“
const
引用”实际上是“对const
的引用”,也就是说你不能改变它们引用的对象的值。它们被声明为const int&
或int const&
而不是int&不过。
The statement
icr=y;
does not make the reference refer toy
; it assigns the value ofy
to the variable thaticr
refers to,i
.References are inherently
const
, that is you can't change what they refer to. There are 'const
references' which are really 'references toconst
', that is you can't change the value of the object they refer to. They are declaredconst int&
orint const&
rather thanint& const
though.什么是常量引用(不是对常量的引用)
常量引用实际上是对常量的引用。
常量引用/对常量的引用表示为:
它基本上意味着,您不能修改引用所引用的类型对象的值。
例如:
尝试通过 const 引用修改变量
j
的值(分配1
),i
将导致错误:不更改引用,它分配引用引用的类型的值。
除了初始化时绑定的变量之外,不能引用任何其他变量。
第一条语句将值
y
赋给i
第二条语句分配值
99
给i
What is a constant reference (not a reference to a constant)
A Constant Reference is actually a Reference to a Constant.
A constant reference/ Reference to a constant is denoted by:
It basically means, you cannot modify the value of type object to which the Reference Refers.
For Example:
Trying to modify value(assign
1
) of variablej
through const reference,i
will results in error:Doesn't change the reference, it assigns the value of the type to which the reference refers.
References cannot be made to refer any other variable than the one they are bound to at Initialization.
First statement assigns the value
y
toi
Second statement assigns the value
99
toi
此代码格式错误:
参考:C++17 [dcl.ref]/1:
该规则已存在于 C++ 的所有标准化版本中。因为代码格式不正确:
编译器应该拒绝该程序;如果没有,则可执行文件的行为完全未定义。
注意:不确定其他答案怎么没有提到这一点......没有人可以访问编译器?
This code is ill-formed:
Reference: C++17 [dcl.ref]/1:
This rule has been present in all standardized versions of C++. Because the code is ill-formed:
The compiler should reject the program; and if it doesn't, the executable's behaviour is completely undefined.
NB: Not sure how none of the other answers mentioned this yet... nobody's got access to a compiler?
我猜你所说的“常量引用”实际上是指“对常量数据的引用”。另一方面,指针可以是常量指针(指针本身是常量,而不是它指向的数据)、指向常量数据的指针,或两者兼而有之。
By "constant reference" I am guessing you really mean "reference to constant data". Pointers on the other hand, can be a constant pointer (the pointer itself is constant, not the data it points to), a pointer to constant data, or both.
正如在另一个答案中提到的,引用本质上是常量。
一旦使用对象初始化了引用,就无法解除该引用与其所引用的对象的绑定。引用的作用就像别名一样。
当你声明一个 const 引用时,它只不过是一个引用 const 对象的引用。
上面的声明性语句(例如
const
和int
)正在确定引用所引用的对象的可用功能。更清楚地说,我想向您展示相当于const
引用的pointer
;所以上面这行代码在其工作方式上相当于一个const引用。 此外,还有最后一点我想提一下;
因此当您这样做时,您将收到错误;
这条规则有一个例外。如果引用被声明为 const,那么您也可以使用文字对其进行初始化;
As it mentioned in another answers, a reference is inherently const.
Once you initialized a reference with an object, you can't unbound this reference with its object it refers to. A reference works just like an alias.
When you declare a
const
reference, it is nothing but a reference which refers to a const object.The declarative sentences above like
const
andint
is determining the available features of the object which will be referenced by the reference. To be more clear, I want to show you thepointer
equivalent of aconst
reference;So the above line of code is equivalent to a
const
reference in its working way. Additionally, there is a one last point which I want to mention;So when you do this, you are going to get an error;
This rule has one exception. If the reference is declared as const, then you can initialize it with literals as well;
首先,我认为
int&const icr=i;
只是int& icr = i
,修饰符'const'没有任何意义(它只是意味着你不能使引用引用其他变量)。其次,常量引用意味着你不能通过引用来改变变量的值。
第三,常量引用可以绑定右值。编译器将创建一个临时变量来绑定引用。
First I think
int&const icr=i;
is justint& icr = i
, Modifier 'const' makes no sense(It just means you cannot make the reference refer to other variable).Second, constant reference just means you cannot change the value of variable through reference.
Third, Constant references can bind right-value. Compiler will create a temp variable to bind the reference.