什么是恒定参考? (不是对常量的引用)

发布于 2024-12-04 17:40:59 字数 483 浏览 1 评论 0原文

为什么常量引用的行为方式与常量指针不同,以便我可以实际更改它们指向的对象?它们确实看起来像是另一个简单的变量声明。我为什么要使用它们?

这是我运行的一个简短示例,它编译并运行时没有错误:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

喜爱纠缠 2024-12-11 17:40:59

最明确的答案。
是否“X&” const x”有什么意义吗?

不,这是废话

要了解上述声明的含义,请从右到左阅读:
“x 是对 X 的 const 引用”。但这是多余的——参考文献
始终是 const,从某种意义上说,您永远无法重新设置引用
使其引用不同的对象。绝不。有或没有
常量。

换句话说,“X& const x”在功能上等同于“X& x”。
由于在 & 之后添加 const 没有任何好处,所以
不应该添加它:它会让人们感到困惑——const 会让一些人
人们认为 X 是 const,就好像你说“const X&” x”。

The clearest answer.
Does “X& const x” make any sense?

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”.

香草可樂 2024-12-11 17:40:59

语句 icr=y; 不会使引用指向 y;它将y 的值分配给icr 引用的变量i

引用本质上是 const,也就是说您无法更改它们所引用的内容。有一些“const引用”实际上是“对const的引用”,也就是说你不能改变它们引用的对象的值。它们被声明为 const int&int const& 而不是 int&不过。

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.

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.

孤寂小茶 2024-12-11 17:40:59

什么是常量引用(不是对常量的引用)
常量引用实际上是对常量的引用

常量引用/对常量的引用表示为:

int const &i = j; //or Alternatively
const int &i = j;
i = 1;            //Compilation Error

它基本上意味着,您不能修改引用所引用的类型对象的值。
例如:
尝试通过 const 引用修改变量 j 的值(分配 1),i 将导致错误:

分配只读引用“i”


icr=y;          // Can change the object it is pointing to so it's not like a const pointer...
icr=99;

不更改引用,它分配引用引用的类型的值。
除了初始化时绑定的变量之外,不能引用任何其他变量。

第一条语句y赋给i
第二条语句分配99i

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:

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:

assignment of read-only reference ‘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 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 to i
Second statement assigns the value 99 to i

乖乖兔^ω^ 2024-12-11 17:40:59

此代码格式错误:

int&const icr=i;

参考:C++17 [dcl.ref]/1:

除非引入了 cv 限定符,否则 Cv 限定引用的格式不正确
通过使用 typedef-namedecltype-specifier,在这种情况下,cv 限定符将被忽略。

该规则已存在于 C++ 的所有标准化版本中。因为代码格式不正确:

  • 您不应该使用它,并且
  • 没有关联的行为。

编译器应该拒绝该程序;如果没有,则可执行文件的行为完全未定义。

注意:不确定其他答案怎么没有提到这一点......没有人可以访问编译器?

This code is ill-formed:

int&const icr=i;

Reference: C++17 [dcl.ref]/1:

Cv-qualified references are ill-formed except when the cv-qualifiers are introduced
through the use of a typedef-name or decltype-specifier, in which case the cv-qualifiers are ignored.

This rule has been present in all standardized versions of C++. Because the code is ill-formed:

  • 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?

贱人配狗天长地久 2024-12-11 17:40:59

我猜你所说的“常量引用”实际上是指“对常量数据的引用”。另一方面,指针可以是常量指针(指针本身是常量,而不是它指向的数据)、指向常量数据的指针,或两者兼而有之。

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.

oО清风挽发oО 2024-12-11 17:40:59

正如在另一个答案中提到的,引用本质上是常量。

int &ref = obj;

一旦使用对象初始化了引用,就无法解除该引用与其所引用的对象的绑定。引用的作用就像别名一样。

当你声明一个 const 引用时,它只不过是一个引用 const 对象的引用。

const int &ref = obj;

上面的声明性语句(例如 constint)正在确定引用所引用的对象的可用功能。更清楚地说,我想向您展示相当于 const 引用的 pointer

const int *const ptr = &obj;

所以上面这行代码在其工作方式上相当于一个const引用。 此外,还有最后一点我想提一下;

引用必须仅使用对象进行初始化

因此当您这样做时,您将收到错误;

int  &r = 0; // Error: a nonconst reference cannot be initialized to a literal

这条规则有一个例外。如果引用被声明为 const,那么您也可以使用文字对其进行初始化;

const int  &r = 0; // a valid approach

As it mentioned in another answers, a reference is inherently 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 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 *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;

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 int  &r = 0; // a valid approach
甜味超标? 2024-12-11 17:40:59

首先,我认为 int&const icr=i; 只是 int& icr = i,修饰符'const'没有任何意义(它只是意味着你不能使引用引用其他变量)。

const int x = 10;
// int& const y = x; // Compiler error here

其次,常量引用意味着你不能通过引用来改变变量的值。

const int x = 10;
const int& y = x;
//y = 20; // Compiler error here

第三,常量引用可以绑定右值。编译器将创建一个临时变量来绑定引用。

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

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).

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文