Namespaces
Variants
Views
Actions

std::enable_shared_from_this::enable_shared_from_this

From cppreference.com
< cpp‎ | memory‎ | enable shared from this
Revision as of 17:23, 2 November 2012 by P12bot (Talk | contribs)

 
 
 
 
 

Template:ddcl list begin <tr class="t-dcl-sep"><td></td><td></td><td></td></tr>

<tr class="t-dcl ">

<td >
constexpr enable_shared_from_this() noexcept;
</td>

<td > (1) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl-sep"><td></td><td></td><td></td></tr>

<tr class="t-dcl ">

<td >
enable_shared_from_this(const enable_shared_from_this<T>&obj) noexcept;
</td>

<td > (2) </td> <td class="t-dcl-nopad"> </td> </tr> Template:ddcl list end

Constructs new enable_shared_from_this object.

Contents

Parameters

obj - an enable_shared_from_this to copy

Exceptions

noexcept specification:  
noexcept
  

Example

#include <memory>
 
struct Foo : public std::enable_shared_from_this<Foo> {
    Foo() {}  // implicitly calls enable_shared_from_this constructor
    std::shared_ptr<Foo> getFoo() { return shared_from_this(); }
};
 
int main() {
    std::shared_ptr<Foo> pf1(new Foo);
    auto pf2 = pf1->getFoo();  // shares ownership of object with pf1
}


See also

Template:cpp/memory/dcl list shared ptr