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); 
    } 
 
  

要求

页眉: <内存>

命名空间: std

请参见

参考

enable_shared_from_this 类