enable_shared_from_this 类

帮助生成 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();
    };

参数

  • Ty
    由共享指针控制的类型。

备注

模板类用作公共基类简化创建 shared_ptr 类 对象添加自己的派生类型的对象:

class derived
    : public enable_shared_from_this<derived>
    {
    };

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

构造函数、析构函数和赋值运算符保护帮助防止意外使用有误。 模板参数类型必须是 Ty 派生类型的类。

要求

页眉: <内存>

命名空间: std

请参见

参考

enable_shared_from_this::shared_from_this

shared_ptr 类