std::unique_ptr
Da cppreference.com.
![]() |
Questa pagina è stata tradotta in modo automatico dalla versione in ineglese della wiki usando Google Translate.
La traduzione potrebbe contenere errori e termini strani. Muovi il puntatore sopra al testo per vedere la versione originale. Puoi aiutarci a correggere gli gli errori. Per ulteriori istruzioni clicca qui. |
Elemento definito nell'header <memory>
|
||
template< class T, |
(1) | (dal C++11) |
template < class T, |
(2) | (dal C++11) |
std::unique_ptr
è un puntatore intelligente che:Original:
std::unique_ptr
is a smart pointer that: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.
- conserva la proprietà esclusiva di un oggetto attraverso un puntatore, eOriginal:retains sole ownership of an object through a pointer, andThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - distrugge la punta-di opporsi quando il
unique_ptr
esce dall'ambito.Original:destroys the pointed-to object when theunique_ptr
goes out of scope.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
unique_ptr
non è copiabile o copia-assegnabili, due istanze di unique_ptr
non può gestire lo stesso oggetto. Un non-const unique_ptr
può trasferire la proprietà dell'oggetto gestito in un altro unique_ptr
. Un const std::unique_ptr non può essere trasferito, limitando la durata dell'oggetto è riuscito a l'ambito in cui è stato creato il puntatore. Quando il unique_ptr
è distrutto, dispone di dell'oggetto attraverso Deleter
.Original:
unique_ptr
is not copyable or copy-assignable, two instances of unique_ptr
cannot manage the same object. A non-const unique_ptr
can transfer the ownership of the managed object to another unique_ptr
. A const std::unique_ptr cannot be transferred, limiting the lifetime of the managed object to the scope in which the pointer was created. When the unique_ptr
is destroyed, it disposes of the object through Deleter
.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.
Ci sono due versioni di
1) std::unique_ptr
:Original:
There are two versions of
std::unique_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.
gestisce la durata di un singolo oggetto, ad esempio assegnato con il nuovo
2) Original:
manages the lifetime of a single object, e.g. allocated with new
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.
gestisce la durata di un array con una lunghezza di esecuzione, ad esempio allocato con new []
Original:
manages the lifetime of an array with a runtime length, e.g. allocated with new[]
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.
Gli usi tipici di
std::unique_ptr
includonoOriginal:
Typical uses of
std::unique_ptr
includeThe 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.
- fornire sicurezza eccezione a classi e funzioni che gestiscono gli oggetti con tempo di vita dinamico, garantendo l'eliminazione sia uscita normale e l'uscita attraverso eccezione.Original:providing exception safety to classes and functions that handle objects with dynamic lifetime, by guaranteeing deletion on both normal exit and exit through exception.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- passando la proprietà degli oggetti di proprietà unico con durata di vita dinamico in funzioniOriginal:passing ownership of uniquely-owned objects with dynamic lifetime into functionsThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- acquisire la proprietà degli oggetti di proprietà unico con durata di vita dinamico dalle funzioniOriginal:acquiring ownership of uniquely-owned objects with dynamic lifetime from functionsThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- come il tipo di elemento in movimento-aware contenitori, come std::vector, che contengono puntatori a oggetti allocati dinamicamente, ad esempio Se il comportamento polimorfico si desideraOriginal:as the element type in move-aware containers, such as std::vector, which hold pointers to dynamically-allocated objects, e.g. if polymorphic behavior is desiredThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
[modifica] Membri tipi
Membro tipo
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 |
pointer | std::remove_reference<D>::type::pointer se quel tipo esiste, altrimenti T *
Original: std::remove_reference<D>::type::pointer if that type exists, otherwise T* The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
element_type | T , il tipo di oggetto gestito da questo unique_ptr Original: T , the type of the object managed by this unique_ptr The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
deleter_type | Deleter , l'oggetto funzione o il riferimento lvalue di funzionare o per oggetto funzione, di essere chiamato dal distruttoreOriginal: Deleter , the function object or lvalue reference to function or to function object, to be called from the destructorThe text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
[modifica] Membri funzioni
costruisce un nuovo unique_ptr Original: constructs a new unique_ptr The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (metodo pubblico) | |
distrugge l'oggetto gestito se questo è presente Original: destructs the managed object if such is present The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (metodo pubblico) | |
assegna il unique_ptr Original: assigns the unique_ptr The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (metodo pubblico) | |
Original: Modifiers The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. | |
restituisce un puntatore all'oggetto gestito e rilascia la proprietà Original: returns a pointer to the managed object and releases the ownership The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (metodo pubblico) | |
sostituisce l'oggetto gestito Original: replaces 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. (metodo pubblico) | |
scambia gli oggetti gestiti 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. (metodo pubblico) | |
Original: Observers The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. | |
restituisce un puntatore all'oggetto gestito Original: returns a pointer to 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. (metodo pubblico) | |
restituisce il deleter che viene utilizzato per la distruzione dell'oggetto gestito Original: returns the deleter that is used for destruction 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. (metodo pubblico) | |
controlla se è associato oggetto gestito Original: checks if there is associated managed object The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (metodo pubblico) | |
Original: Single-object version, unique_ptr<T> The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. | |
dereferenzia puntatore all'oggetto gestito Original: dereferences pointer to 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. (metodo pubblico) | |
Original: Array version, unique_ptr<T[]> The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. | |
fornisce l'accesso indicizzato alla matrice gestita Original: provides indexed access to the managed array 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] Non membri funzioni
confronto ad un altro o con unique_ptr nullptr Original: compares to another unique_ptr or with nullptr The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (funzione di modello) | |
(C++11) |
specializzata l'algoritmo 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. (funzione di modello) |
[modifica] Helper classi
(C++11) |
sostegno hash per std::unique_ptr Original: hash support for std::unique_ptr The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (classe modello di specializzazione) |
[modifica] Esempio
#include <iostream> #include <memory> struct Foo { Foo() { std::cout << "Foo::Foo\n"; } ~Foo() { std::cout << "Foo::~Foo\n"; } void bar() { std::cout << "Foo::bar\n"; } }; void f(const Foo &foo) { std::cout << "f(const Foo&)\n"; } int main() { std::unique_ptr<Foo> p1(new Foo); // p1 owns Foo if (p1) p1->bar(); { std::unique_ptr<Foo> p2(std::move(p1)); // now p2 owns Foo f(*p2); p1 = std::move(p2); // ownership returns to p1 std::cout << "destroying p2...\n"; } if (p1) p1->bar(); // Foo instance is destroyed when p1 goes out of scope }
Output:
Foo::Foo Foo::bar f(const Foo&) destroying p2... Foo::bar Foo::~Foo