enable_shared_from_this::shared_from_this
shared_ptr を生成します。
shared_ptr<Ty> shared_from_this();
shared_ptr<const Ty> shared_from_this() const;
解説
各メンバー関数は、*(Ty*)this を所有する shared_ptr クラス オブジェクトを返します。
使用例
// std_tr1__memory__shared_from_this.cpp
// compile with: /EHsc
#include <memory>
#include <iostream>
struct base
: public std::enable_shared_from_this<base>
{
int val;
};
int main()
{
std::shared_ptr<base> sp0(new base);
std::shared_ptr<base> sp1 = sp0->shared_from_this();
sp0->val = 3;
std::cout << "sp1->val == " << sp1->val << std::endl;
return (0);
}
必要条件
ヘッダー : <memory>
名前空間: std