std::unique_ptr
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. |
Defined in header <memory>
|
||
template< class T, |
(1) | (seit C++11) |
template < class T, |
(2) | (seit C++11) |
std::unique_ptr
ist ein Smart-Pointer, der:
- Alleineigentum eines Objekts über einen Zeiger besitzt, und
- den Zeiger zerstört sobald der
unique_ptr
out of scope geht.
unique_ptr
ist nicht kopierbar oder Kopie zuweisbar. Zwei Instanzen von unique_ptr
können nicht das gleiche Objekt verwalten. Ein nicht-const unique_ptr
kann das Eigentum an dem verwalteten Objekt zu einem anderen unique_ptr
übertragen. Ein const std::unique_ptr können nicht übertragen werden, begrenzen die Lebensdauer des verwalteten Objekts auf den Umfang, in dem der Zeiger angelegt wurde. Wenn der unique_ptr
zerstört wird, verfügt er über das Objekt durch Deleter
.Original:
unique_ptr
is not copyable or copy-assignable, two instances of unique_ptr
cannot manage the same object. A non-const unique_ptr
can transfer the ownership of the managed object to another unique_ptr
. A const std::unique_ptr cannot be transferred, limiting the lifetime of the managed object to the scope in which the pointer was created. When the unique_ptr
is destroyed, it disposes of the object through Deleter
.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.
Es gibt zwei Versionen von
1) std::unique_ptr
:Original:
There are two versions of
std::unique_ptr
: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.
verwaltet die Lebensdauer eines einzelnen Objekts, z. B. zugeordnet mit neuen
2) Original:
manages the lifetime of a single object, e.g. allocated with new
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.
verwaltet die Lebensdauer eines Arrays mit einer Laufzeit Länge, z. B. zugeordnet mit new []
Original:
manages the lifetime of an array with a runtime length, e.g. allocated with new[]
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.
Typische Anwendungen von
std::unique_ptr
gehörenOriginal:
Typical uses of
std::unique_ptr
includeThe 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.
- Bereitstellung Ausnahme Sicherheit auf Klassen und Funktionen, die Objekte zu behandeln mit dynamischer Lebensdauer, durch die Gewährleistung Deletion sowohl normale Ausfahrt und Ausfahrt durch Ausnahme .Original:providing exception safety to classes and functions that handle objects with dynamic lifetime, by guaranteeing deletion on both normal exit and exit through exception.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- vorbei Eigentum einzigartig eigene Objekte mit dynamischer Lebensdauer in FunktionenOriginal:passing ownership of uniquely-owned objects with dynamic lifetime into functionsThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- Erwerb des Eigentums von einzigartig-prozentige Objekte mit dynamischen Lebensdauer von FunktionenOriginal:acquiring ownership of uniquely-owned objects with dynamic lifetime from functionsThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- als Elementtyp in Bewegung unterstützende Behältern wie std::vector, die Zeiger halten, um dynamisch zugeordneten Objekte, zB wenn polymorphe Verhalten gewünscht wirdOriginal:as the element type in move-aware containers, such as std::vector, which hold pointers to dynamically-allocated objects, e.g. if polymorphic behavior is desiredThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
[Bearbeiten] Mitglied Typen
Mitglied Typ
Original: Member type The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
Definition |
pointer | std::remove_reference<D>::type::pointer wenn dieser Typ existiert, ansonsten T *
Original: std::remove_reference<D>::type::pointer if that type exists, otherwise T* The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
element_type | T gelang es der Typ des Objekts dieses unique_ptr Original: T , the type of the object managed by this unique_ptr The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
deleter_type | Deleter die Funktion Objekt oder Lvalue Bezugnahme auf Funktion oder Funktion Objekts, vom destructor aufgerufen werdenOriginal: Deleter , the function object or lvalue reference to function or to function object, to be called from the destructorThe text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
[Bearbeiten] Member-Funktionen
erstellt einen neuen unique_ptr (öffentliche Elementfunktion) | |
weist den unique_ptr zu (öffentliche Elementfunktion) | |
Original: Modifiers The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. | |
liefert einen Zeiger auf das verwaltete Objekt und gibt ihn frei (öffentliche Elementfunktion) | |
ersetzt das verwaltete Objekt Original: replaces the managed object The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (öffentliche Elementfunktion) | |
vertauscht die verwalteten Objekte Original: swaps the managed objects The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (öffentliche Elementfunktion) | |
Original: Observers The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. | |
liefert einen Zeiger auf das verwaltete Objekt Original: returns a pointer to the managed object The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (öffentliche Elementfunktion) | |
liefert die Funktion, die zur Zerstörung des verwalteten Objekt verwendet wird Original: returns the deleter that is used for destruction of the managed object The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (öffentliche Elementfunktion) | |
prüft, ob verwalteten Objekt verknüpft ist Original: checks if there is associated managed object The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (öffentliche Elementfunktion) | |
Original: Single-object version, unique_ptr<T> The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. | |
dereferenziert den Zeiger auf das verwaltete Objekt (öffentliche Elementfunktion) | |
Original: Array version, unique_ptr<T[]> The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. | |
bietet indizierten Zugriff auf das verwaltete Array Original: provides indexed access to the managed array 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] Non-Member-Funktionen
vergleicht zu einem anderen oder mit unique_ptr nullptr Original: compares to another unique_ptr or with nullptr The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (Funktions-Template) | |
(C++11) |
spezialisiert die std::swap Algorithmus Original: specializes the std::swap algorithm The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (Funktions-Template) |
[Bearbeiten] Helper-Klassen
(C++11) |
Hash-Unterstützung für std::unique_ptr Original: hash support for std::unique_ptr The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (class Template-Spezialisierung) |
[Bearbeiten] Beispiel
#include <iostream> #include <memory> struct Foo { Foo() { std::cout << "Foo::Foo\n"; } ~Foo() { std::cout << "Foo::~Foo\n"; } void bar() { std::cout << "Foo::bar\n"; } }; void f(const Foo &foo) { std::cout << "f(const Foo&)\n"; } int main() { std::unique_ptr<Foo> p1(new Foo); // p1 owns Foo if (p1) p1->bar(); { std::unique_ptr<Foo> p2(std::move(p1)); // now p2 owns Foo f(*p2); p1 = std::move(p2); // ownership returns to p1 std::cout << "destroying p2...\n"; } if (p1) p1->bar(); // Foo instance is destroyed when p1 goes out of scope }
Output:
Foo::Foo Foo::bar f(const Foo&) destroying p2... Foo::bar Foo::~Foo