关于 const 引用和临时对象的问题
A problem about const references and temporary objects
我从 C++ Primer 了解到,当我将 const 引用绑定到非 const 对象时,该引用将绑定到一个临时对象,该对象的值是该非 const 对象。
int a = 1;
const int &b = a;
a = 2;
std::cout<<b;
根据我的理解,将创建一个值为 a
的临时 const int
对象,并用它初始化 b
,所以,就好像我写了此代码:
int a = 1;
const int x = a;
const int &b = x;
a = 2;
std::cout<<b;
第一个代码打印 2,而第二个代码打印 1。为什么?为什么 const 引用 b
的值随着 a
的变化而变化,而它实际上绑定到临时 const 对象而不是直接绑定到 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;
在第二个片段中,b 是对 x 的引用。 (注意 x 不是对 a 的引用)。所以改变a不会改变x,所以它不会改变b。
你混淆了 C++ 初级读物所说的两个不同的东西。
首先,您使用术语 "const reference" 来表示本身不变的引用。这不是 C++ 入门中术语 "const reference" 的意思。正如它所说:
TERMINOLOGY: CONST REFERENCE IS A REFERENCE TO CONST
C++ programmers tend to cavalier in their use of the term const reference. Striclty speaking, what is meant by "const reference" is "reference to const". ... This usage is so common that we will follow it in this book as well.
其次,您将涉及不同基本类型(double
和 const int
)的示例和规则与涉及相同基本类型(int
和 const int
)的示例混淆了.
试试这个以获得您描述的效果:
double a = 42.0;
const int &b = a;
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
. You can whatever you want to do to a
but if you were
to do:
int c = 7;
&b = c;
i.e., change the reference of b
, you will get an error. b
will hold whatever the value of whatever 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,
In the second snippet, b is a reference to x. (Note that x is NOT a
reference to a). So changing a doesn't change x, and so it doesn't
change b.
您的变量 b
与 a
无关。它只是
related/referenced 到 x
就像第一个代码到 a
一样。
在这里,对 a
的任何更改都不会影响 x
。因此,这证明了
回答 ;)
最佳。
I understood, a temporary const int object whos value is a will be
created and b will be initialized with it,
你错了。此代码段中没有创建任何临时对象
int a = 1;
const int &b = a;
而且在C++标准中甚至没有指定是否为引用b分配内存。
您应该将引用 b
视为变量 a 的别名。
由于引用将对象 a
引用为常量对象,因此您不能使用引用来更改对象 a。尽管如此,对象 a 被声明为非常量对象。所以你可以改变它的价值,比如
a = 2;
但您不能使用像
这样的参考来更改它
b = 2;
如果引用声明为
,您可以使用引用来更改对象 a 的值
int &b = a;
在这种情况下,这两个语句的结果
a = 2;
和
b = 2;
将等效。
至于这段代码
int a = 1;
const int x = a;
const int &b = x;
a = 2;
std::cout<<b;
然后常量 x
被赋值给变量 a
的值的副本。 x
和 a
是两个不同的对象,它们占用不同的内存范围。
引用 b
被声明为对对象 x
的引用。
const int &b = x;
因此更改对象 a
不会影响常量 x
的值。常量 x
既不能直接更改,也不能通过使用引用 b
.
更改
我从 C++ Primer 了解到,当我将 const 引用绑定到非 const 对象时,该引用将绑定到一个临时对象,该对象的值是该非 const 对象。
int a = 1;
const int &b = a;
a = 2;
std::cout<<b;
根据我的理解,将创建一个值为 a
的临时 const int
对象,并用它初始化 b
,所以,就好像我写了此代码:
int a = 1;
const int x = a;
const int &b = x;
a = 2;
std::cout<<b;
第一个代码打印 2,而第二个代码打印 1。为什么?为什么 const 引用 b
的值随着 a
的变化而变化,而它实际上绑定到临时 const 对象而不是直接绑定到 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;
在第二个片段中,b 是对 x 的引用。 (注意 x 不是对 a 的引用)。所以改变a不会改变x,所以它不会改变b。
你混淆了 C++ 初级读物所说的两个不同的东西。
首先,您使用术语 "const reference" 来表示本身不变的引用。这不是 C++ 入门中术语 "const reference" 的意思。正如它所说:
TERMINOLOGY: CONST REFERENCE IS A REFERENCE TO CONST
C++ programmers tend to cavalier in their use of the term const reference. Striclty speaking, what is meant by "const reference" is "reference to const". ... This usage is so common that we will follow it in this book as well.
其次,您将涉及不同基本类型(double
和 const int
)的示例和规则与涉及相同基本类型(int
和 const int
)的示例混淆了.
试试这个以获得您描述的效果:
double a = 42.0;
const int &b = a;
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
. You can whatever you want to do toa
but if you were to do:int c = 7; &b = c;
i.e., change the reference of
b
, you will get an error.b
will hold whatever the value of whatevera
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,
In the second snippet, b is a reference to x. (Note that x is NOT a reference to a). So changing a doesn't change x, and so it doesn't change b.
您的变量
b
与a
无关。它只是 related/referenced 到x
就像第一个代码到a
一样。 在这里,对a
的任何更改都不会影响x
。因此,这证明了 回答 ;)最佳。
I understood, a temporary const int object whos value is a will be created and b will be initialized with it,
你错了。此代码段中没有创建任何临时对象
int a = 1;
const int &b = a;
而且在C++标准中甚至没有指定是否为引用b分配内存。
您应该将引用 b
视为变量 a 的别名。
由于引用将对象 a
引用为常量对象,因此您不能使用引用来更改对象 a。尽管如此,对象 a 被声明为非常量对象。所以你可以改变它的价值,比如
a = 2;
但您不能使用像
这样的参考来更改它b = 2;
如果引用声明为
,您可以使用引用来更改对象 a 的值int &b = a;
在这种情况下,这两个语句的结果
a = 2;
和
b = 2;
将等效。
至于这段代码
int a = 1;
const int x = a;
const int &b = x;
a = 2;
std::cout<<b;
然后常量 x
被赋值给变量 a
的值的副本。 x
和 a
是两个不同的对象,它们占用不同的内存范围。
引用 b
被声明为对对象 x
的引用。
const int &b = x;
因此更改对象 a
不会影响常量 x
的值。常量 x
既不能直接更改,也不能通过使用引用 b
.