I have a structure that manages objects that derive from a base class Entity
, but does not control their lifetimes. I want this structure to be given weak pointers like weak_ptr<Entity>
so that it can know if the object has been destroyed elsewhere.
However, outside of the managing structure where the shared pointer lives, I want the shared pointer to be the more specific shared_ptr<SpecificEntity>
(SpecificEntity uses Entity as a base class).
Is there a way to accomplish this, or something like it?
The only difference between weak_ptr and shared_ptr is that the weak_ptr allows the reference counter object to be kept after the actual object was freed. As a result, if you keep a lot of shared_ptr in a std::set the actual objects will occupy a lot of memory if they are big enough.
The weak_ptr class template stores a "weak reference" to an object that's already managed by a shared_ptr. To access the object, a weak_ptr can be converted to a shared_ptr using the shared_ptr constructor or the member function lock.
All the instances point to the same object, and share access to one "control block" that increments and decrements the reference count whenever a new shared_ptr is added, goes out of scope, or is reset. When the reference count reaches zero, the control block deletes the memory resource and itself.
By using a weak_ptr , you can create a shared_ptr that joins to an existing set of related instances, but only if the underlying memory resource is still valid. A weak_ptr itself does not participate in the reference counting, and therefore, it cannot prevent the reference count from going to zero.
That's very possible. You can always convert a shared_ptr<Derived>
to a shared_ptr<Base>
implicitly, and for the other direction you have std::static_pointer_cast
and std::dynamic_pointer_cast
, which do what you'd expect – i.e. you end up with a new pointer of different type that shares ownership with the original pointer. Example:
std::shared_ptr<Base> p(new Derived);
std::shared_ptr<Derived> q = std::static_pointer_cast<Derived>(p);
std::shared_ptr<Base> r = q;
Or, more C++11-style:
auto p0 = std::make_shared<Derived>();
std::shared_ptr<Base> p = p0;
auto q = std::static_pointer_cast<Derived>(p);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With