Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top