0

我知道如果我这样做:

class Obj
{
public:
    int* nine;
};

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

那么Obj1's 和Obj2'snine将指向同一个 s 8,但它们会共享同一个指针吗?IE:

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

将指向Obj2还是将 一直 指向?nineNecronine8

4

1 回答 1

4

Obj2 的 9 会指向 Necronine,还是会一直指向 8?

它将一直指向 8。当执行此行时: Obj Obj2 = Obj1; // 每个对象都有自己的指针,该指针被复制到其中value(copy),仅此而已。obj1.nineobj2.nine

于 2009-10-30T02:20:25.110 回答