共用方式為


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 Class 物件。

範例

 

// 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

請參閱

參考

enable_shared_from_this Class