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. |
| Dclar dans l'en-tte <memory>
|
||
template< class T > class weak_ptr; |
(depuis C++11) | |
std::weak_ptr est un pointeur intelligent qui dtient une rfrence non-propritaire (faible) un objet qui est gr par std::shared_ptr. Il doit tre converti en std::shared_ptr afin d'accder l'objet rfrenc.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 modlise une proprit temporaire: quand un objet doit tre accessible uniquement si il 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 proprit temporaire. Si le std::shared_ptr original est dtruit ce moment, la dure de vie de l'objet est prorog jusqu'au premier std::shared_ptr temporaire est dtruite ainsi .
En outre, std::weak_ptr est utilise pour briser les rfrences 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.
Types de membres
| Type du membre | Dfinition |
element_type
|
T
|
Fonctions membres
cre 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) | |
dtruit 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. | |
libre la proprit de l'objet gr 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 grs 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 grent 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) | |
vrifie si l'objet rfrenc a dj 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) | |
cre un shared_ptr qui gre l'objet rfrencOriginal: 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 propritaire base 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) | |
Fonctions annexes
(C++11) |
l'algorithme spcialis 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 gnrique) |
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();
}
Rsultat :
42
gw is expired