简体   繁体   English

关于 const 引用和临时对象的问题

[英]A problem about const references and temporary objects

I understood from C++ Primer that when I bind a const reference to a non const object, the reference is bound to a temporary object whos value is the non const object.我从 C++ Primer 中了解到,当我将 const 引用绑定到非 const 对象时,该引用将绑定到其值为非 const 对象的临时对象。

int a = 1;
const int &b = a;
a = 2;
std::cout<<b;

According to what I understood, a temporary const int object whos value is a will be created and b will be initialized with it, so, it is as if I wrote this code:根据我的理解,将创建a值为 a 的临时const int对象,并用它初始化b ,所以,就好像我写了这段代码:

int a = 1;
const int x = a;
const int &b = x;
a = 2;
std::cout<<b;

The first code prints 2, while the second prints 1. Why?第一个代码打印 2,而第二个代码打印 1。为什么? Why did the value of the const reference b change with changing a while it is actually bound to a temporary const object not to a directly?为什么常引用的值b不断变化改变a ,而它实际上是绑定到一个临时const对象不是a直接?

You are confusing two different things the C++ primer is saying.你混淆了 C++ 入门所说的两种不同的东西。

First, you are using the term "const reference" to mean a reference that is itself constant.首先,您使用术语“常量引用”来表示本身是常量的引用。 This is not what the C++ primer means by the term "const reference".这不是 C++ 入门中术语“const 引用”的意思。 As it says:正如它所说:

TERMINOLOGY: CONST REFERENCE IS A REFERENCE TO CONST术语:const 引用是对const 的引用
C++ programmers tend to cavalier in their use of the term const reference . C++ 程序员在使用术语const reference 时倾向于傲慢。 Striclty speaking, what is meant by " const reference " is " reference to const ". Striclty讲,什么叫“常引用”指的是“参考常量”。 ... This usage is so common that we will follow it in this book as well. ... 这种用法非常普遍,我们也将在本书中遵循它。

Second, you are confusing examples and rules involving different base types ( double and const int ) with examples involving the same base type ( int and const int ).其次,您将涉及不同基类型( doubleconst int )的示例和规则与涉及相同基类型( intconst int )的示例混淆了。

Try this to get the effect you are describing:试试这个以获得您所描述的效果:

double a = 42.0;
const int &b = a;
int a = 1;
const int x = a;  // x is a copy of a
const int &b = x;  // b is a reference to x, not a
a = 2;
std::cout<<b;

In the second snippet, b is a reference to x.在第二个片段中,b 是对 x 的引用。 (Note that x is NOT a reference to a). (请注意,x 不是对 a 的引用)。 So changing a doesn't change x, and so it doesn't change b.所以改变a不会改变x,所以它不会改变b。

I understood, a temporary const int object whos value is a will be created and b will be initialized with it,我明白,将创建一个值为 a 的临时 const int 对象,并用它初始化 b,

You are wrong.你错了。 Neither temporary object is created in this code snippet在此代码片段中没有创建临时对象

int a = 1;
const int &b = a;

Moreover it is even unspecified in the C++ Standard whether a memory allocated for the reference b.此外,在 C++ 标准中甚至未指定是否为引用 b 分配了内存。

You should consider the reference b as an alias for the variable a.您应该将引用b视为变量 a 的别名。

As the reference refers the object a as a constant object you may not use the reference to change the object a.由于引用将对象a称为常量对象,因此您不能使用引用来更改对象 a。 Nevertheless the object a is declared as a non-constant object.尽管如此,对象 a 被声明为非常量对象。 So you may change its value like所以你可以改变它的价值

a = 2;

but you may not change it using the reference like但你不能使用像这样的参考来改变它

b = 2;

You could use the reference to change the value of the object a if the reference was declared like如果引用声明如下,您可以使用引用来更改对象 a 的值

int &b = a;

In this case the result of these two statements在这种情况下,这两个语句的结果

a = 2;

and

b = 2;

will be equivalent.将是等效的。

As for this code snippet至于这个代码片段

int a = 1;
const int x = a;
const int &b = x;
a = 2;
std::cout<<b;

then the constant x is assigned with a copy of the value of the variable a .然后为常量x分配变量a的值的副本。 x and a are two different objects that occupy different extents of memory. xa是两个不同的对象,它们占用不同的内存范围。

The reference b is declared as a reference to the object x .引用b被声明为对对象x的引用。

const int &b = x;

So changing the object a does not influence on the value of the constant x .所以改变对象a不会影响常数x的值。 The constant x may not be changed neither directly nor by using the reference b .常量x既不能直接更改,也不能使用引用b更改。

Hello @Mason,你好@梅森,

 int a = 1; const int &b = a; a = 2; std::cout<<b;

Here, the variable b is bound to a constant address or a "reference" which is, a .这里,变量b绑定到一个常量地址或“引用”,即a You can whatever you want to do to a but if you were to do:你可以对a做任何你想做的事情,但如果你要这样做:

 int c = 7; &b = c;

ie , change the reference of b , you will get an error.,更改b的引用,您将收到错误消息。 b will hold whatever the value of whatever a as. b将保持a as 的任何值。 This is kind of how pass/call by reference works.这就是通过引用传递/调用的工作方式。

 int a = 1; const int x = a; const int &b = x; a = 2; std::cout<<b;

Here, just like @cigien said,在这里,就像@cigien 所说的,

In the second snippet, b is a reference to x.在第二个片段中,b 是对 x 的引用。 (Note that x is NOT a reference to a). (请注意,x 不是对 a 的引用)。 So changing a doesn't change x, and so it doesn't change b.所以改变a不会改变x,所以它不会改变b。

your variable b has no relation to a .您的变量ba无关。 It is only related/referenced to x just like in the first code it was to a .它仅与x相关/引用,就像在第一个代码中它与a Here, any change to a does not affect x .这里,对a任何更改都不会影响x Hence, this justifies the answer ;)因此,这证明了答案;)

Best.最好的事物。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM