更新 : 2007 年 11 月
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::tr1