Do shallow copies share pointers? (C++)

322 views Asked by At

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?

1

There are 1 answers

2
Khaled Alshaya On BEST ANSWER

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.