Share via


enable_shared_from_this Class

Helps generate a shared_ptr.

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

protected:
    enable_shared_from_this();
    enable_shared_from_this(const enable_shared_from_this&);
    enable_shared_from_this& operator=(const enable_shared_from_this&);
    ~enable_shared_from_this();
    };

Parameters

  • Ty
    The type controlled by the shared pointer.

Remarks

The template class can be used as a public base class to simplify creating shared_ptr Class objects that own objects of the derived type:

class derived
    : public enable_shared_from_this<derived>
    {
    };

shared_ptr<derived> sp0(new derived);
shared_ptr<derived> sp1 = sp0->shared_from_this();

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.

Requirements

Header: <memory>

Namespace: std

See Also

Reference

shared_ptr Class

Other Resources

<memory> Members