在两个语句中创建引用变量
Creating a reference variable in two statements
int x;
int& foo = x;
// foo is now a reference to x so this sets x to 56
foo = 56;
如何将语句 int& foo = x;
拆分为两个语句?
通过拆分,我的意思是使用两个语句,如下例所示:
int y;
int* ptr = &y;
我可以将 int* ptr = &y
拆分为两个声明指针的语句。
int* ptr;
ptr = &y; //then assigning the pointer to point to y
如何对引用做类似的事情?我也在寻找关于为什么或为什么不的解释?
你就是做不到。将引用视为 const
指针:
int& foo = x
等同于 int * const foo = &x
.
这就是为什么你不能重新分配一个引用,或者声明一个没有值的新引用。
你没有办法做到这一点。
引用应该是常量,因此您只能对其进行初始化。
不,必须初始化引用,因为声明它支持保证没有指向任何地方的引用。
有空指针这种东西,没有空引用这种东西。参考 必须 有它所指的东西。它必须初始化。
int& r; // error
这是指针和引用之间的主要区别之一,也是可能更喜欢采用引用参数(必须有效)而不是指针参数(可以为空)的原因之一。
不,这是做不到的,只有少数情况下可以省略引用的初始化器,来自草案 C++ 标准部分 8.5.3
[dcl.init.ref]:
The initializer can be omitted for a reference only in a parameter
declaration (8.3.5), in the declaration of a function return type, in
the declaration of a class member within its class definition (9.2),
and where the extern specifier is explicitly used. [ Example:
int& r1; // error: initializer missing
extern int& r2; // OK
—end example ]
至于为什么我们发现无法从 The Design and Evolution of C++ 重置引用的以下基本原理:
It is not possible to change what a reference refers to after
initialization. That is once a C++ reference is initialized it cannot
be made to refer to a different object later; it cannot be re-bound. I
had in the past been bitten by Algol68 references where r1=r2
can
either assign through r1 to the object referred to or assign to a new
reference value to r1 (re-binding r1) depending on the type of r2. I
wanted to avoid such problems in C++.
不,你不能。
一种可能的解决方法是使用 std::boost::optional<T&>
或简单地使用 T*
int x;
boost::optional<int&> foo; // or int* foo = nullptr;
foo = x; // foo = &x;
// foo is now a 'reference' to x
*foo = 56; // now, we have x == 56
int x;
int& foo = x;
// foo is now a reference to x so this sets x to 56
foo = 56;
如何将语句 int& foo = x;
拆分为两个语句?
通过拆分,我的意思是使用两个语句,如下例所示:
int y;
int* ptr = &y;
我可以将 int* ptr = &y
拆分为两个声明指针的语句。
int* ptr;
ptr = &y; //then assigning the pointer to point to y
如何对引用做类似的事情?我也在寻找关于为什么或为什么不的解释?
你就是做不到。将引用视为 const
指针:
int& foo = x
等同于 int * const foo = &x
.
这就是为什么你不能重新分配一个引用,或者声明一个没有值的新引用。
你没有办法做到这一点。 引用应该是常量,因此您只能对其进行初始化。
不,必须初始化引用,因为声明它支持保证没有指向任何地方的引用。
有空指针这种东西,没有空引用这种东西。参考 必须 有它所指的东西。它必须初始化。
int& r; // error
这是指针和引用之间的主要区别之一,也是可能更喜欢采用引用参数(必须有效)而不是指针参数(可以为空)的原因之一。
不,这是做不到的,只有少数情况下可以省略引用的初始化器,来自草案 C++ 标准部分 8.5.3
[dcl.init.ref]:
The initializer can be omitted for a reference only in a parameter declaration (8.3.5), in the declaration of a function return type, in the declaration of a class member within its class definition (9.2), and where the extern specifier is explicitly used. [ Example:
int& r1; // error: initializer missing extern int& r2; // OK
—end example ]
至于为什么我们发现无法从 The Design and Evolution of C++ 重置引用的以下基本原理:
It is not possible to change what a reference refers to after initialization. That is once a C++ reference is initialized it cannot be made to refer to a different object later; it cannot be re-bound. I had in the past been bitten by Algol68 references where
r1=r2
can either assign through r1 to the object referred to or assign to a new reference value to r1 (re-binding r1) depending on the type of r2. I wanted to avoid such problems in C++.
不,你不能。
一种可能的解决方法是使用 std::boost::optional<T&>
或简单地使用 T*
int x;
boost::optional<int&> foo; // or int* foo = nullptr;
foo = x; // foo = &x;
// foo is now a 'reference' to x
*foo = 56; // now, we have x == 56