std::weak_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 > class weak_ptr; |
(seit C++11) | |
std::weak_ptr ist ein Smart-Pointer, die eine nicht-besitzenden ("schwache") Verweis auf ein Objekt, das durch std::shared_ptr verwaltet hält. Er muss std::shared_ptr umgewandelt werden, um den Zugriff auf das referenzierte Objekt .Original:
std::weak_ptr is a smart pointer that holds a non-owning ("weak") reference to an object that is managed by std::shared_ptr. It must be converted to std::shared_ptr in order to access the referenced object.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.
std::weak_ptr Modelle temporären Eigentümer:. wenn ein Objekt, auf das zugegriffen nur wenn es vorhanden sein muss, und es kann jederzeit von einer anderen gelöscht werden, std::weak_ptr wird verwendet, um das Objekt zu verfolgen, und sie ist an std::shared_ptr umgewandelt vorübergehende Besitz übernehmen. Wenn die ursprüngliche std::shared_ptr zu dieser Zeit zerstört wird, das Objekt Lebensdauer verlängert wird, bis die temporäre std::shared_ptr als auch .
Original:
std::weak_ptr models temporary ownership: when an object needs to be accessed only if it exists, and it may be deleted at any time by someone else, std::weak_ptr is used to track the object, and it is converted to std::shared_ptr to assume temporary ownership. If the original std::shared_ptr is destroyed at this time, the object's lifetime is extended until the temporary std::shared_ptr is destroyed as well.
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.
Zusätzlich std::weak_ptr dient Zirkelverweise von std::shared_ptr brechen .
Original:
In addition, std::weak_ptr is used to break circular references of std::shared_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.
[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 |
| element_type | T |
[Bearbeiten] Member-Funktionen
| schafft eine neue weak_ptrOriginal: creates a new weak_ptrThe text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (öffentliche Elementfunktion) | |
| zerstört eine weak_ptrOriginal: destroys a weak_ptrThe text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (öffentliche Elementfunktion) | |
| ordnet die weak_ptrOriginal: assigns the weak_ptrThe text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (ö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. | |
| löst das Eigentum des verwalteten Objekts Original: releases the ownership 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) | |
| Swaps 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. | |
| gibt die Anzahl der shared_ptr Objekte, die das Objekt verwalten Original: returns the number of shared_ptr objects that manage the 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 das referenzierte Objekt wurde bereits gelöscht Original: checks whether the referenced object was already deleted The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (öffentliche Elementfunktion) | |
| schafft eine shared_ptr, die das referenzierte Objekt verwaltetOriginal: creates a shared_ptr that manages the referenced objectThe text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (öffentliche Elementfunktion) | |
| bietet Inhaber-basierte Sortierung der schwachen Zeiger Original: provides owner-based ordering of weak pointers 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
| (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] Beispiel
Veranschaulicht Wie Sperre verwendet wird, um die Gültigkeit des Zeigers zu gewährleisten .
Original:
Demonstrates how lock is used to ensure validity of the pointer.
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.
#include <iostream> #include <memory> std::weak_ptr<int> gw; void f() { if (auto spt = gw.lock()) { // Has to be copied into a shared_ptr before usage std::cout << *spt << "\n"; } else { std::cout << "gw is expired\n"; } } int main() { { auto sp = std::make_shared<int>(42); gw = sp; f(); } f(); }
Output:
42 gw is expired