分配参考
考虑以下声明:
Object *a;
Object *b;
有两种将 a
分配给 b
的方法,按值或按引用:
a = b; // by reference
*a = *b; // by value
如果声明是:(
Object &a;
Object &b;
我知道不可能在没有初始化它们,只是为了显示类型)
也有两种将 a
分配给 b
的方法,就像指针一样,通过引用或通过值。
这段代码:
a = b;
要做哪一个?有可能让它做相反的事情吗? (使用不同的语法?)
Consider the following declarations:
Object *a;
Object *b;
There are two ways of assigning a
to b
, by value or by reference:
a = b; // by reference
*a = *b; // by value
If the declarations are:
Object &a;
Object &b;
(I know its not possible to declare them without initializing them, its just to show the types)
There are two ways of assigning a
to b
too, just like with pointers, by reference or by value.
Which one this code:
a = b;
is going to do? Is it possible to make it do the opposite one? (with a different syntax?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这会覆盖所引用的对象。
不,不可能重新绑定引用以使其引用不同的对象。如果您需要这样做,请改用指针。
That overwrites the object referred to.
And no, it is not possible to rebind a reference so that it refers to a different object. If you need to do that, use a pointer instead.