std::enable_shared_from_this::shared_from_this
De cppreference.com
< cpp | memory | enable shared from this
![]() |
This page has been machine-translated from the English version of the wiki using Google Translate.
The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
shared_ptr<T> shared_from_this(); |
(1) | |
shared_ptr<T const> shared_from_this() const; |
(2) | |
Devuelve una std::shared_ptr<T> que comparte la propiedad de *this con todo std::shared_ptr<T> existentes que se refieren 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.
Contenido |
[editar] Notas
Antes de llamar
shared_from_this
, debe haber por lo menos un std::shared_ptr<T> p
que posee *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.
[editar] Valor de retorno
std::shared_ptr<T> que comparte la propiedad de *this con pre-existentes 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.
[editar] Ejemplo
#include <iostream> #include <memory> struct Foo : public std::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"; }
Salida:
Foo::Foo pf2 is gone Foo::~Foo
[editar] Ver también
(C++11) |
smart pointer with shared object ownership semantics (plantilla de clase) |