std::enable_shared_from_this allows an object t that is currently managed by a std::shared_ptr named pt to safely generate additional std::shared_ptr instances pt1, pt2, ... that all share ownership of t with pt.

The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions.

Inheriting from std::enable_shared_from_this<T> provides a type T with a member function shared_from_this. If an object t of type T is managed by a std::shared_ptr<T> named pt, then calling T::shared_from_this will return a new std::shared_ptr<T> that shares ownership of t with pt.

The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions.

Also note that enable_shared_from_this provides an alternative to an expression like std::shared_ptr<T>(this), which is likely to result in this being destructed more than once by multiple owners that are unaware of eachother.

The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions.

A common implementation for enable_shared_from_this is to hold a weak reference (such as std::weak_ptr) to this. The constructors of std::shared_ptr can detect presence of a enable_shared_from_this base and share ownership with the existing std::shared_ptrs, instead of assuming the pointer is not managed by anyone else.

The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions.