std::enable_shared_from_this::shared_from_this
Da 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) | |
Retorna uma std::shared_ptr<T> que compartilha a propriedade de *this com toda std::shared_ptr<T> existente que se referem 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.
Índice |
[editar] Notas
Antes de chamar
shared_from_this
, deve haver pelo menos um std::shared_ptr<T> p
que possui *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> propriedade que partes de *this com pré-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] Exemplo
#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"; }
Saída:
Foo::Foo pf2 is gone Foo::~Foo
[editar] Veja também
(C++11) |
smart pointer with shared object ownership semantics (modelo de classe) |