浅拷贝共享指针吗? (C++)
我知道如果我这样做:
class Obj
{
public:
int* nine;
};
Obj Obj1; //Awesome name
int eight = 8;
Obj1.nine = &eight;
Obj Obj2 = Obj1; //Another Awesome name
那么 Obj1
和 Obj2
的 9
将指向相同的 8
,但是它们会共享相同的指针吗?即:
int Necronine = 9;
Obj1.nine = &Necronine;
Obj2.nine == ???
Obj2
的 9
会指向 Necronine
,还是仍指向 8
?
I know that if I do something like this:
class Obj
{
public:
int* nine;
};
Obj Obj1; //Awesome name
int eight = 8;
Obj1.nine = &eight;
Obj Obj2 = Obj1; //Another Awesome name
then Obj1
's and Obj2
's nine
s will point to the same 8
, but will they share the same pointer? I.e.:
int Necronine = 9;
Obj1.nine = &Necronine;
Obj2.nine == ???
will Obj2
's nine
point to Necronine
, or will it remain pointing at 8
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它将保持指向 8。当执行此行时:
对象Obj2 = 对象1; // 每个对象都有自己的指针
obj1.nine
的value(copy)
被复制到obj2.9
中,仅此而已。It will remain pointing at 8. When this line is executed:
Obj Obj2 = Obj1; // every object has his own pointer
the
value(copy)
ofobj1.nine
is copied intoobj2.nine
and thats it.