std::enable_shared_from_this
Aus cppreference.com
![]() |
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. |
definiert in Header <memory>
|
||
template< class T > class enable_shared_from_this; |
(seit C++11) | |
std::enable_shared_from_this
kann ein Objekt t
, die derzeit von einem std::shared_ptr pt
sicher generieren zusätzliche std::shared_ptr Fällen pt1, pt2, ...
dass alle Aktienbesitz t
mit pt
benannt ist geschafft .Original:
std::enable_shared_from_this
allows an object t
that is currently managed by a std::shared_ptr named pt
to safely generate additional std::shared_ptr instances pt1, pt2, ...
that all share ownership of t
with pt
.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.
Erben von
std::enable_shared_from_this<T>
stellt eine Art T
mit einer Member-Funktion shared_from_this
. Wenn ein Objekt t
vom Typ T
durch eine std::shared_ptr<T> Namen verwaltet wird pt
, dann ruft T::shared_from_this
wieder eine neue std::shared_ptr<T>, dass Aktien das Eigentum an t
mit pt
.Original:
Inheriting from
std::enable_shared_from_this<T>
provides a type T
with a member function shared_from_this
. If an object t
of type T
is managed by a std::shared_ptr<T> named pt
, then calling T::shared_from_this
will return a new std::shared_ptr<T> that shares ownership of t
with pt
.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.
Beachten Sie, dass vor dem Aufruf
shared_from_this
auf ein Objekt t
, muss es eine std::shared_ptr die t
besitzt sein .Original:
Note that prior to calling
shared_from_this
on an object t
, there must be a std::shared_ptr that owns t
.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.
Beachten Sie auch, dass
enable_shared_from_this
eine Alternative zu einem Ausdruck wie std::shared_ptr<T>(this), die wahrscheinlich in this mehr als einmal zerstört durch mehrere Eigentümer, die nichts von einander sind das Ergebnis ist bietet .Original:
Also note that
enable_shared_from_this
provides an alternative to an expression like std::shared_ptr<T>(this), which is likely to result in this being destructed more than once by multiple owners that are unaware of eachother.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] Member-Funktionen
konstruiert eine enabled_shared_from_this Objekt Original: constructs an enabled_shared_from_this object The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (geschützt Member-Funktion) | |
zerstört eine enable_shared_from_this Objekt Original: destroys an enable_shared_from_this object The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (geschützt Member-Funktion) | |
gibt einen Verweis auf this Original: returns a reference to this The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (geschützt Member-Funktion) | |
gibt einen shared_ptr das Eigentum an *this Aktien Original: returns a shared_ptr which shares ownership of *this The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (öffentliche Elementfunktion) |
[Bearbeiten] Notes
Eine gemeinsame Implementierung für
enable_shared_from_this
ist es, einen schwachen Verweis (z. B. std::weak_ptr) an this halten. Die Konstrukteure von std::shared_ptr kann die Anwesenheit eines enable_shared_from_this
Basis und Aktienbesitz mit den bestehenden std::shared_ptrs erkennen, anstatt davon auszugehen, der Zeiger nicht von jemand anderem verwaltet .Original:
A common implementation for
enable_shared_from_this
is to hold a weak reference (such as std::weak_ptr) to this. The constructors of std::shared_ptr can detect presence of a enable_shared_from_this
base and share ownership with the existing std::shared_ptrs, instead of assuming the pointer is not managed by anyone else.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 <memory> #include <iostream> struct Good: std::enable_shared_from_this<Good> { std::shared_ptr<Good> getptr() { return shared_from_this(); } }; struct Bad { std::shared_ptr<Bad> getptr() { return std::shared_ptr<Bad>(this); } ~Bad() { std::cout << "Bad::~Bad() called\n"; } }; int main() { // Good: the two shared_ptr's share the same object std::shared_ptr<Good> gp1(new Good); std::shared_ptr<Good> gp2 = gp1->getptr(); std::cout << "gp2.use_count() = " << gp2.use_count() << '\n'; // Bad, each shared_ptr thinks it's the only owner of the object std::shared_ptr<Bad> bp1(new Bad); std::shared_ptr<Bad> bp2 = bp1->getptr(); std::cout << "bp2.use_count() = " << bp2.use_count() << '\n'; } // UB: double-delete of Bad
Output:
gp2.use_count() = 2 bp2.use_count() = 1 Bad::~Bad() called Bad::~Bad() called *** glibc detected *** ./test: double free or corruption
[Bearbeiten] Siehe auch
(C++11) |
intelligenter Zeiger, der geteilten Objektbesitz abbildet (Klassen-Template) |