如何更改 C++ 的变量参考是指?
如果我有这个:
int a = 2;
int b = 4;
int &ref = a;
如何使 ref
在此代码之后引用 b
?
If I have this:
int a = 2;
int b = 4;
int &ref = a;
How can I make ref
refer to b
after this code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
这是不可能的,这是设计使然的。引用不能反弹。
This is not possible, and that's by design. References cannot be rebound.
在 C++11 中,出现了新的 std::reference_wrapper。
这对于在容器中存储引用也很有用。
With C++11 there is the new(ish) std::reference_wrapper.
This is also useful for storing references in containers.
您无法重新分配引用,但如果您正在寻找提供与此类似功能的东西,您可以使用指针代替。
您可以使用以下方式获取或设置指针内的值:
You can't reassign a reference, but if you're looking for something that would provide similar abilities to this you can do a pointer instead.
You can get or set the value within pointer with:
从形式上来说,这是不可能的,因为这是设计所禁止的。实际上,如果你想破解它,这是可能的。
引用存储为指针,因此只要您知道如何获取其地址,就可以随时更改它指向的位置。同样,当您无权访问时,您也可以更改 const 变量、const 成员变量甚至私有成员变量的值。
例如,以下代码更改了类 A 的 const 私有成员引用:
程序输出:
可以看到 a.i1 最初指向 i,更改后,它指向j。
然而,这样做被认为是危险的,因此不推荐,因为它违背了参考的最初目的。 C/C++语言中
reference
的设计目的是禁止在编程层面改变其内容。因此,如果最终您需要更改其值,那么您应该考虑将该变量定义为指针。此外,这里提出的解决方案假设引用被实现为指针(在大多数编译器实现中都是如此,但不是全部)。因此,当底层实现不是指针时,它将不起作用(在这种情况下会导致程序崩溃)。Formally speaking, that is impossible as it is forbidden by design. Practically speaking, that is possible if you want to hack it.
A references is stored as a pointer, so you can always change where it points to as long as you know how to get its address. Similarly, you can also change the value of const variables, const member variables or even private member variables when you don't have access to.
For example, the following code has changed class A's const private member reference:
Program output:
As you can see that a.i1 initially points to i, after the change, it points to j.
However, doing so is considered as dangerous and thus unrecommended, because it defeats the original purpose of reference. The design purpose of
reference
in C/C++ language is to forbid alterations of its content at programming level. So if it ends up that you need to alter its value, then you should consider define that variable as a pointer instead. Moreover, the solution proposed here is assuming that the reference is implemented as a pointer (which is true in most of the compiler implementations but not all). So it will NOT work when the underlying implementation is not a pointer (and it will cause your program to crash in that case).您无法重新分配参考。
You cannot reassign a reference.
以你想要的方式这是不可能的。 C++ 只是不允许您重新绑定引用所指向的内容。
然而,如果你想使用欺骗,你几乎可以用一个新的范围来模拟它(永远在真实的程序中这样做):
That's not possible in the way you want. C++ just doesn't let you rebind what a reference points to.
However if you want to use trickery you can almost simulate it with a new scope (NEVER do this in a real program):
您可以使用新的放置非常容易地创建参考包装器:
You can make a reference wrapper very easy using the placement new:
这是可能的。因为在底层,引用是一个指针。
以下代码将打印“hello world”
This is possible. Because under the hood, reference is a pointer.
The following code will print "hello world"
正如其他答案所说,这是不可能的。
但是,如果您将引用存储在
class
或struct
中,则可以使用 Placement new 重新创建整个内容,从而重新绑定引用。正如 @HolyBlackCat 指出的,不要忘记使用std::launder
来访问 re -创建的对象或使用从放置新返回的指针。考虑我的示例:demo
输出为:
It is impossible, as other answers state.
However, if you store your reference in a
class
orstruct
, you could re-create the whole thing using placement new, so the reference is re-bound. As @HolyBlackCat noted, don't forget to usestd::launder
to access the re-created object or use the pointer returned from the placement new. Consider my example:demo
The output is:
虽然这是一个坏主意,因为它违背了使用引用的目的,但可以直接更改引用
Although its a bad idea as it defeats the purpose of using references, it is possible to change the reference directly