开发者

Assigning references

开发者 https://www.devze.com 2023-04-12 05:32 出处:网络
Consider the following declarations: Object *a; Object *b; There are two ways of assigning a to b, by value or by reference:

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?)


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号