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. |
<metanoindex/>
| Defined in header <memory>
|
||
template< class T, class Deleter = std::default_delete<T> > class unique_ptr; |
(1) | (dal C++11) |
template < class T, class Deleter > class unique_ptr<T[],Deleter>; |
(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_ptresce dall'ambito.Original:destroys the pointed-to object when theunique_ptrgoes 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
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.
1)
gestisce la durata di un singolo oggetto, ad esempio assegnato con il nuovo
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.
2)
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 asstd::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.
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_ptrOriginal: T, the type of the object managed by this unique_ptrThe 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. |
Membri funzioni
Template:cpp/memory/unique ptr/dcl list constructorTemplate:cpp/memory/unique ptr/dcl list destructorTemplate:cpp/memory/unique ptr/dcl list operator=Template:cpp/memory/unique ptr/dcl list releaseTemplate:cpp/memory/unique ptr/dcl list resetTemplate:cpp/memory/unique ptr/dcl list swapTemplate:cpp/memory/unique ptr/dcl list getTemplate:cpp/memory/unique ptr/dcl list get deleterTemplate:cpp/memory/unique ptr/dcl list operator boolTemplate:cpp/memory/unique ptr/dcl list operator*Template:cpp/memory/unique ptr/dcl list operator at
Original: Modifiers The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. | |
Original: Observers The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. | |
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. | |
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. |
Non membri funzioni
Template:cpp/memory/unique ptr/dcl list operator cmpTemplate:cpp/memory/unique ptr/dcl list swap2Helper classi
Template:cpp/memory/unique ptr/dcl list hashEsempio
#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