浅いコピーはポインターを共有しますか? (C ++)
-
22-07-2019 - |
質問
次のような操作を行うと、
class Obj
{
public:
int* nine;
};
Obj Obj1; //Awesome name
int eight = 8;
Obj1.nine = &eight;
Obj Obj2 = Obj1; //Another Awesome name
then Obj1
と Obj2
の nine
は同じ 8
を指しますが、彼らは同じポインタを共有していますか?つまり:
int Necronine = 9;
Obj1.nine = &Necronine;
Obj2.nine == ???
Obj2
の nine
は Necronine
を指しますか、それとも 8
を指し続けますか?
解決
Obj2のネクロニンに対する9つのポイントは、 または、8を指したままになりますか?
8を指したままになります。この行が実行されると:
Obj Obj2 = Obj1; //すべてのオブジェクトには独自のポインターがあります
obj1.nine
の value(copy)
が obj2.nine
にコピーされ、それがそれです。
所属していません StackOverflow