浅拷贝共享指针吗? (C++)

发布于 2024-08-10 07:25:03 字数 475 浏览 2 评论 0原文

我知道如果我这样做:

class Obj
{
public:
    int* nine;
};

Obj Obj1; //Awesome name
int eight = 8;
Obj1.nine = &eight;
Obj Obj2 = Obj1; //Another Awesome name

那么 Obj1Obj29 将指向相同的 8,但是它们会共享相同的指针吗?即:

int Necronine = 9;
Obj1.nine = &Necronine;
Obj2.nine == ???

Obj29 会指向 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 nines 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不弃不离 2024-08-17 07:25:03

将 Obj2 的九点指向 Necronine,
还是仍指向 8?

它将保持指向 8。当执行此行时:
对象Obj2 = 对象1; // 每个对象都有自己的指针
obj1.ninevalue(copy) 被复制到 obj2.9 中,仅此而已。

will Obj2's nine point to Necronine,
or will it remain pointing at 8?

It will remain pointing at 8. When this line is executed:
Obj Obj2 = Obj1; // every object has his own pointer
the value(copy) of obj1.nine is copied into obj2.nine and thats it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文