std::enable_shared_from_this

Da cppreference.com.
< cpp‎ | memory

 
 
Utilità libreria
Tipo di supporto (basic types, RTTI, type traits)
Gestione della memoria dinamica
La gestione degli errori
Programma di utilità
Funzioni variadic
Data e ora
Funzione oggetti
initializer_list(C++11)
bitset
hash(C++11)
Gli operatori relazionali
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>=
Coppie e tuple
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.
pair
tuple(C++11)
piecewise_construct_t(C++11)
piecewise_construct(C++11)
Swap, in avanti e spostare
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.
swap
forward(C++11)
move(C++11)
move_if_noexcept(C++11)
declval(C++11)
 
Gestione della memoria dinamica
Basso livello di gestione della memoria
Allocatori
Original:
Allocators
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
allocator
allocator_traits(C++11)
allocator_arg_t(C++11)
allocator_arg(C++11)
uses_allocator(C++11)
scoped_allocator_adaptor(C++11)
Non inizializzata stoccaggio
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.
uninitialized_copy
uninitialized_copy_n(C++11)
uninitialized_fill
uninitialized_fill_n
raw_storage_iterator
get_temporary_buffer
return_temporary_buffer
Puntatori intelligenti
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.
unique_ptr(C++11)
shared_ptr(C++11)
weak_ptr(C++11)
auto_ptr(deprecato)
owner_less(C++11)
enable_shared_from_this(C++11)
bad_weak_ptr(C++11)
default_delete(C++11)
Garbage collection supporto
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.
declare_reachable(C++11)
undeclare_reachable(C++11)
declare_no_pointers(C++11)
undeclare_no_pointers(C++11)
pointer_safety(C++11)
get_pointer_safety(C++11)
Varie
Original:
Miscellaneous
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
pointer_traits(C++11)
addressof(C++11)
align(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
Protetto funzioni 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=
Funzioni membro pubbliche
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
 
Elemento definito nell'header <memory>
template<class T >class enable_shared_from_this;
(dal C++11)
std::enable_shared_from_this permette un t oggetto che è attualmente gestito da un std::shared_ptr nome pt per generare in modo sicuro altre istanze std::shared_ptrpt1, pt2, ... che tutti azionariato di t con 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.
Eredità da std::enable_shared_from_this<T> fornisce un T tipo con shared_from_this funzione membro. Se un oggetto di tT tipo è gestito da un std::shared_ptr<T> nome pt, quindi T::shared_from_this chiamata restituirà un nuovo std::shared_ptr<T> che la proprietà di parti t con 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.
Notare che prima di chiamare shared_from_this su un t oggetto, ci deve essere un std::shared_ptr che possiede 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.
Si noti inoltre che enable_shared_from_this fornisce un'alternativa a un'espressione come std::shared_ptr<T>(this), che è suscettibile di provocare this stati distrutti più di una volta da più proprietari che non sono a conoscenza della vicenda.
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.

Indice

[modifica]Membri funzioni

costruisce un oggetto 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.

(protetto funzione membro)
distrugge un oggetto 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.

(protetto funzione membro)
restituisce un riferimento 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.

(protetto funzione membro)
restituisce un shared_ptr che condivide la proprietà di *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.

(metodo pubblico)

[modifica]Note

Un'implementazione comune per enable_shared_from_this è quello di tenere un riferimento debole (come std::weak_ptr) a this. I costruttori di std::shared_ptr in grado di rilevare la presenza di una base enable_shared_from_this e gli assetti proprietari con i std::shared_ptrs esistenti, invece di assumere il puntatore non è gestito da nessun altro.
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.

[modifica]Esempio

#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(){returnstd::shared_ptr<Bad>(this);} ~Bad(){std::cout<<"Bad::~Bad() called\n";}};   int main(){// Good: the two shared_ptr's share the same objectstd::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 objectstd::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

Output:

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

[modifica]Vedi anche

smart pointer with shared object ownership semantics
(classe template)[modifica]
close