std::weak_ptr
De 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. |
| Défini dans l'entête <memory>
|
||
| template< class T > class weak_ptr; |
(depuis C++11) | |
std::weak_ptr est un pointeur intelligent qui détient une référence non-propriétaire («faible») à un objet qui est géré par std::shared_ptr. Il doit être converti en std::shared_ptr afin d'accéder à l'objet référencé.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 modèles propriété temporaire: quand un objet doit être accessible uniquement si elle existe, et il peut être supprimé à tout moment par quelqu'un d'autre, std::weak_ptr est utilisé pour suivre l'objet, et il est converti en std::shared_ptr d'assumer la propriété temporaire. Si le std::shared_ptr original est détruit à ce moment, la durée de vie de l'objet est prorogé jusqu'au premier std::shared_ptr temporaire est détruite ainsi .
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.
En outre, std::weak_ptr est utilisée pour briser les références circulaires de std::shared_ptr .
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.
[modifier] Types de membres
| Type de membre
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 |
[modifier] Les fonctions membres
| crée un nouveau 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. (fonction membre publique) | |
| détruit un 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. (fonction membre publique) | |
| assigne la 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. (fonction membre publique) | |
Original: Modifiers The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. | |
| libère la propriété de l'objet géré 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. (fonction membre publique) | |
| permute les objets gérés 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. (fonction membre publique) | |
Original: Observers The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. | |
| retourne le nombre d'objets qui gèrent shared_ptr l'objet 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. (fonction membre publique) | |
| vérifie si l'objet référencé a déjà été supprimé 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. (fonction membre publique) | |
| crée un shared_ptr qui gère l'objet référencéOriginal: 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. (fonction membre publique) | |
| fournit propriétaire basée ordre des pointeurs faibles 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. (fonction membre publique) | |
[modifier] Tiers fonctions
| (C++11) |
l'algorithme spécialisé std::swap 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. (fonction générique) |
[modifier] Exemple
Montre comment verrou est utilisé pour assurer la validité du pointeur .
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(); }
Résultat :
42 gw is expired