Espaços nominais
Variantes
Acções

std::enable_shared_from_this

Da cppreference.com
< cpp‎ | memory

 
 
Biblioteca de utilitários
Digite apoio (basic types, RTTI, type traits)
Gerenciamento de memória dinâmica
De tratamento de erros
Utilidades do programa
Variadic funções
Data e hora
Objetos de função
(C++11)
Os operadores relacionais
Original:
Relational operators
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
rel_ops::operator!=
rel_ops::operator>
rel_ops::operator<=
rel_ops::operator>=
Pares e tuplas
Original:
Pairs and tuples
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
(C++11)
Troque, avançar e avançar
Original:
Swap, forward and move
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
(C++11)
(C++11)
(C++11)
 
Gerenciamento de memória dinâmica
Gerenciamento de memória de baixo nível
Alocadores
Original:
Allocators
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Uninitialized armazenamento
Original:
Uninitialized storage
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Ponteiros inteligentes
Original:
Smart pointers
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
(C++11)
(C++11)
(C++11)
(obsoleta)
(C++11)
enable_shared_from_this
(C++11)
Apoio a coleta de lixo
Original:
Garbage collection support
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Diversos
Original:
Miscellaneous
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
(C++11)
(C++11)
C Library
Original:
C Library
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
 
std::enable_shared_from_this
Protegido funções de membro
Original:
Protected member functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
enable_shared_from_this::enable_shared_from_this
enable_shared_from_this::~enable_shared_from_this
enable_shared_from_this::operator=
Membro funções públicas
Original:
Public member functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
enable_shared_from_this::shared_from_this
 
Definido no cabeçalho <memory>
template< class T > class enable_shared_from_this;
(desde C++11)
std::enable_shared_from_this permite uma t objeto que atualmente é administrada por um std::shared_ptr chamado pt com segurança gerar instâncias adicionais std::shared_ptr pt1, pt2, ... que todos acionária de t com pt.
Original:
std::enable_shared_from_this allows an object t that is currently managed by a std::shared_ptr named pt to safely generate additional std::shared_ptr instances pt1, pt2, ... that all share ownership of t with pt.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Herdando de std::enable_shared_from_this<T> fornece uma T tipo com um shared_from_this função de membro. Se um objeto de t T tipo é gerido por um std::shared_ptr<T> chamado pt, em seguida, chamar T::shared_from_this irá retornar um novo std::shared_ptr<T> que compartilha a propriedade de t com pt.
Original:
Inheriting from std::enable_shared_from_this<T> provides a type T with a member function shared_from_this. If an object t of type T is managed by a std::shared_ptr<T> named pt, then calling T::shared_from_this will return a new std::shared_ptr<T> that shares ownership of t with pt.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Note-se que antes da chamada shared_from_this numa t objeto, deve haver um std::shared_ptr que possui t.
Original:
Note that prior to calling shared_from_this on an object t, there must be a std::shared_ptr that owns t.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Observe também que enable_shared_from_this fornece uma alternativa para uma expressão como std::shared_ptr<T>(this), o que provavelmente resultará em this sendo destruída mais de uma vez por vários proprietários que desconhecem eachother.
Original:
Also note that enable_shared_from_this provides an alternative to an expression like std::shared_ptr<T>(this), which is likely to result in this being destructed more than once by multiple owners that are unaware of eachother.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Índice

[editar] Funções de membro

constrói um objeto enabled_shared_from_this
Original:
constructs an enabled_shared_from_this object
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(protegido função de membro)
destrói um objeto enable_shared_from_this
Original:
destroys an enable_shared_from_this object
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(protegido função de membro)
retorna uma referência para this
Original:
returns a reference to this
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(protegido função de membro)
retorna um shared_ptr que compartilha a propriedade de *this
Original:
returns a shared_ptr which shares ownership of *this
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] Notas

Uma implementação comum para enable_shared_from_this é manter uma referência fraca (como std::weak_ptr) para this. Os construtores de std::shared_ptr pode detectar a presença de uma base enable_shared_from_this e participação acionária com as std::shared_ptrs existentes, em vez de assumir o ponteiro não é gerenciado por outra pessoa.
Original:
A common implementation for enable_shared_from_this is to hold a weak reference (such as std::weak_ptr) to this. The constructors of std::shared_ptr can detect presence of a enable_shared_from_this base and share ownership with the existing std::shared_ptrs, instead of assuming the pointer is not managed by anyone else.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[editar] Exemplo

#include <memory>
#include <iostream>
 
struct Good: std::enable_shared_from_this<Good>
{
    std::shared_ptr<Good> getptr() {
        return shared_from_this();
    }
};
 
struct Bad {
    std::shared_ptr<Bad> getptr() {
        return std::shared_ptr<Bad>(this);
    }
    ~Bad() { std::cout << "Bad::~Bad() called\n"; }
};
 
int main()
{
    // Good: the two shared_ptr's share the same object
    std::shared_ptr<Good> gp1(new Good);
    std::shared_ptr<Good> gp2 = gp1->getptr();
    std::cout << "gp2.use_count() = " << gp2.use_count() << '\n';
 
    // Bad, each shared_ptr thinks it's the only owner of the object
    std::shared_ptr<Bad> bp1(new Bad);
    std::shared_ptr<Bad> bp2 = bp1->getptr();
    std::cout << "bp2.use_count() = " << bp2.use_count() << '\n';
} // UB: double-delete of Bad

Saída:

gp2.use_count() = 2
bp2.use_count() = 1
Bad::~Bad() called
Bad::~Bad() called
*** glibc detected *** ./test: double free or corruption

[editar] Veja também

smart pointer with shared object ownership semantics
(modelo de classe) [edit]