C++ 什么是常量引用?(不是对常量的引用)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7420780/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is a constant reference? (not a reference to a constant)
提问by Bat0u89
A pretty theoretical question...Why constant references do not behave the same way as constant pointers and 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;
}
回答by Bat0u89
The clearest answer. Does “X& const x” make any sense?
最清楚的答案。 “X& const x”有意义吗?
No, it is nonsense
To find out what the above declaration means, read it right-to-left: “x is a const reference to a X”. But that is redundant — references are always const, in the sense that you can never reseat a reference to make it refer to a different object. Never. With or without the const.
In other words, “X& const x” is functionally equivalent to “X& x”. Since you're gaining nothing by adding the const after the &, you shouldn't add it: it will confuse people — the const will make some people think that the X is const, as if you had said “const X& x”.
不,这是胡说八道
要了解上述声明的含义,请从右到左阅读:“x 是对 X 的常量引用”。但这是多余的——引用始终是常量,从某种意义上说,您永远无法重新设置引用以使其引用不同的对象。绝不。有或没有常量。
换句话说,“X& const x”在功能上等同于“X& x”。由于在 & 后面添加 const 并没有得到任何好处,因此您不应该添加它:它会使人们感到困惑——const 会使一些人认为 X 是 const,就好像您说的是“const X& x”。
回答by mattnewport
The statement icr=y;
does not make the reference refer to y
; it assigns the value of y
to the variable that icr
refers to, i
.
该声明icr=y;
没有使参考参考y
;它将 的值分配给引用,y
的变量。icr
i
References are inherently const
, that is you can't change what they refer to. There are 'const
references' which are really 'references to const
', that is you can't change the value of the object they refer to. They are declared const int&
or int const&
rather than int& const
though.
引用本质const
上是 ,即您无法更改它们所引用的内容。有“const
引用”实际上是“引用const
”,即您无法更改它们引用的对象的值。它们是声明的const int&
,int const&
而不是声明的int& const
。
回答by Alok Save
What is a constant reference (not a reference to a constant)
A Constant Referenceis actually a Reference to a Constant.
什么是恒定的参考(不是恒定的引用)
一个恒定的参考实际上是参考一个常数。
A constant reference/ Reference to a constant is denoted by:
常量引用/对常量的引用表示为:
int const &i = j; //or Alternatively
const int &i = j;
i = 1; //Compilation Error
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 variable j
through const reference, i
will results in error:
这基本上意味着,您不能修改引用所引用的类型对象的值。
例如:
尝试通过 const 引用修改1
变量的value(assign ) j
,i
将导致错误:
assignment of read-only reference ‘i'
只读引用“i”的分配
icr=y; // Can change the object it is pointing to so it's not like a const pointer...
icr=99;
Doesn't change the reference, it assignsthe 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 assignsthe value y
to i
Second statement assignsthe value 99
to i
首先声明受让人值y
到i
第二条语句指定的值99
来i
回答by M.M
This code is ill-formed:
此代码格式错误:
int&const icr=i;
Reference: C++17 [dcl.ref]/1:
参考:C++17 [dcl.ref]/1:
Cv-qualified references are ill-formed except when the cv-qualifiers are introduced through the use of a typedef-nameor decltype-specifier, in which case the cv-qualifiers are ignored.
cv 限定的引用是格式错误的,除非通过使用typedef-name或decltype-specifier引入 cv 限定符,在这种情况下,忽略 cv 限定符。
This rule has been present in all standardized versions of C++. Because the code is ill-formed:
此规则已存在于 C++ 的所有标准化版本中。因为代码格式错误:
- you should not use it, and
- there is no associated behaviour.
- 你不应该使用它,并且
- 没有相关的行为。
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 Ozan Yurtsever
As it mentioned in another answers, a reference is inherently const.
正如在另一个答案中提到的,引用本质上是 const。
int &ref = obj;
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.
当你声明一个 const
引用时,它只不过是一个引用 const 对象的引用。
const int &ref = obj;
The declarative sentences above like const
and int
is determining the available features of the object which will be referenced by the reference. To be more clear, I want to show you the pointer
equivalent of a const
reference;
上面的陈述句喜欢 const
并且int
正在确定将被引用引用的对象的可用特征。更清楚地说,我想向您展示pointer
相当于const
参考的内容;
const int *const ptr = &obj;
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;
所以上面这行代码const
在工作方式上相当于一个引用。此外,还有最后一点我想提一下;
A reference must be initialized only with an object
必须仅使用对象初始化引用
So when you do this, you are going to get an error;
所以当你这样做时,你会得到一个错误;
int &r = 0; // Error: a nonconst reference cannot be initialized to a literal
This rule has one exception. If the reference is declared as const, then you can initialize it with literals as well;
这一规则有一个例外。如果引用声明为 const,那么您也可以使用文字对其进行初始化;
const int &r = 0; // a valid approach
回答by Poodlehat
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.
通过“常量引用”,我猜你的意思是“引用常量数据”。另一方面,指针可以是常量指针(指针本身是常量,而不是它指向的数据)、指向常量数据的指针,或两者兼而有之。
回答by Novice
First I think int&const icr=i;
is just int& icr = i
, Modifier 'const' makes no sense(It just means you cannot make the reference refer to other variable).
首先我认为int&const icr=i;
只是int& icr = i
,修饰符 'const' 没有意义(这只是意味着您不能使引用引用其他变量)。
const int x = 10;
// int& const y = x; // Compiler error here
Second, constant reference just means you cannot change the value of variable through reference.
其次,常量引用只是意味着你不能通过引用来改变变量的值。
const int x = 10;
const int& y = x;
//y = 20; // Compiler error here
Third, Constant references can bind right-value. Compiler will create a temp variable to bind the reference.
第三,常量引用可以绑定右值。编译器将创建一个临时变量来绑定引用。
float x = 10;
const int& y = x;
const int& z = y + 10;
cout << (long long)&x << endl; //print 348791766212
cout << (long long)&y << endl; //print 348791766276
cout << (long long)&z << endl; //print 348791766340