次の方法で共有


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 は、派生クラスの型であることが必要です。

必要条件

ヘッダー : <memory>

名前空間: std

参照

関連項目

enable_shared_from_this::shared_from_this

shared_ptr クラス