Just started working with shared pointers and tried an example program like this:
int x = 10;
shared_ptr<int> ptr = make_shared<int>(x);
*ptr = 11;
cout<< x << " " << *ptr;
The result is 10 11 which didn't make sense to me since ptr was pointing to x and hence the value of x should have been changed to 11. Can someone please explain here? Also I would need the value of x to be changed when a shared pointer to it is changed. Please let me know how I can achieve that as well. Thanks in advance.
Aucun commentaire:
Enregistrer un commentaire