0

Does boost::shared_ptr<int> ptr makes a copy when

*ptr.get() = 5;

suppose, the refcount was 3, after the line above will it stay 3 ? or what will happen? will the new object be allocated?

Uylenburgh
  • 1,277
  • 4
  • 20
  • 46

1 Answers1

1

No, it won't copy, because then the object would no longer be shared.

Also, no need for explicit call to get():

*ptr = 5;

This has no effect on refcount.

(To create a new shared object: ptr = boost::make_shared<int>(5))

sehe
  • 374,641
  • 47
  • 450
  • 633
  • so I can change the value of underlying object without effecting refcount? just all objects that point to `boost::shared_ptr ptr` will still point to `boost::shared_ptr ptr`, just the value it holds will be different, right ? – Uylenburgh Apr 24 '14 at 12:10
  • Yes, @Oleksandra, my answer isn't going to change. For what it's worth, it's exactly the same as with regular pointers (`int*`) in this respect. – sehe Apr 24 '14 at 12:11