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