std::enable_shared_from_this::shared_from_this
Aus 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) | |
Gibt eine std::shared_ptr<T>, dass Aktien das Eigentum an *this mit allen vorhandenen std::shared_ptr<T>, die beziehen sich auf *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.
Inhaltsverzeichnis |
[Bearbeiten] Notes
Vor dem Aufruf
shared_from_this
, sollte es mindestens sein std::shared_ptr<T> p
die *this besitzt .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.
[Bearbeiten] Rückgabewert
std::shared_ptr<T>, dass Aktien das Eigentum an *this mit bereits bestehenden 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.
[Bearbeiten] Beispiel
#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"; }
Output:
Foo::Foo pf2 is gone Foo::~Foo
[Bearbeiten] Siehe auch
(C++11) |
intelligenter Zeiger, der geteilten Objektbesitz abbildet (Klassen-Template) |