std::enable_shared_from_this::shared_from_this
Da cppreference.com.
< cpp | memory | enable shared from this
![]() | Questa pagina è stata tradotta in modo automatico dalla versione in ineglese della wiki usando Google Translate. La traduzione potrebbe contenere errori e termini strani. Muovi il puntatore sopra al testo per vedere la versione originale. Puoi aiutarci a correggere gli gli errori. Per ulteriori istruzioni clicca qui. |
shared_ptr<T> shared_from_this(); | (1) | |
shared_ptr<T const> shared_from_this()const; | (2) | |
Restituisce un std::shared_ptr<T> che la proprietà di parti *this con tutto std::shared_ptr<T> esistenti che fanno riferimento a *this.
Original:
Returns a std::shared_ptr<T> that shares ownership of *this with all existing std::shared_ptr<T> that refer to *this.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Indice |
[modifica]Note
Prima di chiamare
shared_from_this
, ci dovrebbe essere almeno un std::shared_ptr<T>p
che possiede *this.Original:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[modifica]Valore di ritorno
std::shared_ptr<T> che la proprietà di parti *this con pre-esistenti std::shared_ptr<T>s
Original:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[modifica]Esempio
#include <iostream>#include <memory> struct Foo :publicstd::enable_shared_from_this<Foo>{ Foo(){std::cout<<"Foo::Foo\n";} ~Foo(){std::cout<<"Foo::~Foo\n";}std::shared_ptr<Foo> getFoo(){return shared_from_this();}}; int main(){ Foo *f = new Foo;std::shared_ptr<Foo> pf1; {std::shared_ptr<Foo> pf2(f); pf1 = pf2->getFoo();// shares ownership of object with pf2} std::cout<<"pf2 is gone\n";}
Output:
Foo::Foo pf2 is gone Foo::~Foo
[modifica]Vedi anche
(C++11) | smart pointer with shared object ownership semantics (classe template) |