enable_shared_from_this Class


Helps generate a shared_ptr.

template<class Ty>
    class enable_shared_from_this {
    shared_ptr<Ty> shared_from_this();
    shared_ptr<const Ty> shared_from_this() const;

    enable_shared_from_this(const enable_shared_from_this&);
    enable_shared_from_this& operator=(const enable_shared_from_this&);


The type controlled by the shared pointer.

Objects derived from enable_shared_from_this can use the shared_from_this methods in member functions to create shared_ptr owners of the instance that share ownership with existing shared_ptr owners. Otherwise, if you create a new shared_ptr by using this, it is distinct from existing shared_ptr owners, which can lead to invalid references or cause the object to be deleted more than once.

The constructors, destructor, and assignment operator are protected to help prevent accidental misuse. The template argument type Ty must be the type of the derived class.

For an example of usage, see enable_shared_from_this::shared_from_this.


Header: <memory>

Namespace: std