std::weak_ptr
Da 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. |
Definido no cabeçalho <memory> | ||
template<class T >class weak_ptr; | (desde C++11) | |
std::weak_ptr
é um ponteiro inteligente que contém um não-proprietária de referência ("fraco") a um objeto que é gerenciado pelo std::shared_ptr. Ele deve ser convertido para std::shared_ptr para acessar o objeto referenciado.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 modelos de propriedade temporária:. quando um objeto precisa ser acessado somente se ele existe, e ele pode ser excluído a qualquer momento por outra pessoa, std::weak_ptr é usado para rastrear o objeto, e ele é convertido para std::shared_ptr para assumir a propriedade temporária. std::shared_ptr Se o original é destruído neste momento, a vida útil do objeto é prorrogado até o std::shared_ptr temporário é destruído, bem.
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.
Além disso, std::weak_ptr é usado para quebrar as referências circulares 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.
[editar]Tipos de membro
Tipo de membro 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 |
[editar]Funções de membro
cria um novo weak_ptr Original: creates a new weak_ptr The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (função pública membro) | |
destrói uma weak_ptr Original: destroys a weak_ptr The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (função pública membro) | |
atribui o weak_ptr Original: assigns the weak_ptr The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (função pública membro) | |
Original: Modifiers The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. | |
libera a propriedade do objeto gerenciado 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. (função pública membro) | |
troca os objetos gerenciados 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. (função pública membro) | |
Original: Observers The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. | |
retorna o número de objetos shared_ptr que gerenciam o objeto 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. (função pública membro) | |
Verifica se o objeto referenciado já foi excluído 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. (função pública membro) | |
cria um shared_ptr que gerencia o objeto referenciadoOriginal: 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. (função pública membro) | |
fornece-proprietário baseado ordenação dos ponteiros fracos 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. (função pública membro) |
[editar]Não-membros funções
(C++11) | o algoritmo especializado 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. (modelo de função) |
[editar]Exemplo
Demonstra como bloqueio é usado para garantir a validade do ponteiro .
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 usagestd::cout<<*spt <<"\n";}else{std::cout<<"gw is expired\n";}} int main(){{auto sp =std::make_shared<int>(42); gw = sp; f();} f();}
Saída:
42 gw is expired