gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-27 23:34:40 25 4
gpt4 key购买 nike

我知道如果我这样做:

class Obj
{
public:
int* nine;
};

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

然后 Obj1的和Obj2nine s 将指向相同的 8 ,但它们会共享相同的指针吗?即:

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

Obj2nine指向 Necronine , 或者它会继续指向 8

最佳答案

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

它将保持指向 8。执行此行时:Obj Obj2 = Obj1;//每个对象都有自己的指针obj1.ninevalue(copy) 被复制到 obj2.nine 中,就是这样。

关于c++ - 浅拷贝共享指针吗? (C++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1647649/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com