Share via


enable_shared_from_this::shared_from_this

Generates a shared_ptr.

shared_ptr<Ty> shared_from_this();
shared_ptr<const Ty> shared_from_this() const;

Remarks

The member functions each return a shared_ptr Class object that owns *(Ty*)this.

Example

 

// 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); 
    } 
 
sp1->val == 3

Requirements

Header: <memory>

Namespace: std

See Also

Reference

enable_shared_from_this Class