Destructors
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. |
Un distruttore è una funzione membro speciale che viene chiamato quando il ciclo di vita di un oggetto finisce. Lo scopo del distruttore è di liberare le risorse che l'oggetto può aver acquisito durante la sua vita.
Original:
A destructor is a special member function that is called when the lifetime of an object ends. The purpose of the destructor is to free the resources that the object may have acquired during its lifetime.
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.
Indice |
[modifica]Sintassi
~class_name (); | (1) | ||||||||
virtual~class_name (); | (2) | ||||||||
~class_name () = default; | (3) | (dal C++11) | |||||||
~class_name () = delete; | (4) | (dal C++11) | |||||||
[modifica]Spiegazione
# Dichiarazione tipica di un distruttore
Original:
# Typical declaration of a destructor
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.
# Distruttore virtuale è di solito richiesto in una classe base
Original:
# Virtual destructor is usually required in a base class
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.
# Forzare un distruttore di essere generato dal compilatore
Original:
# Forcing a destructor to be generated by the compiler
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.
# Disabilitare il distruttore implicita
Original:
# Disabling the implicit destructor
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.
Il distruttore viene chiamato ogni volta che la vita di un oggetto finisce, che comprende
Original:
The destructor is called whenever an object's lifetime ends, which includes
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.
- programma di cessazione, per gli oggetti con la durata di archiviazione staticaOriginal:program termination, for objects with static storage durationThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - filo di uscita, per gli oggetti con thread-local (dal C++11) durata di conservazioneOriginal:thread exit, for objects with thread-local storage duration (dal C++11)The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - fine del campo di applicazione, per gli oggetti con durata memorizzazione automatica e per i file temporanei la cui vita è stata prorogata di associazione a un riferimentoOriginal:end of scope, for objects with automatic storage duration and for temporaries whose life was extended by binding to a referenceThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - delete-espressione, per gli oggetti con la durata di archiviazione dinamicaOriginal:delete-expression, for objects with dynamic storage durationThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - fine la piena espressione, per provvisori senza nomeOriginal:end of the full expression, for nameless temporariesThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - pila di svolgimento, per gli oggetti con durata di archiviazione automatica quando un'eccezione sfugge loro blocco, non rilevata.Original:stack unwinding, for objects with automatic storage duration when an exception escapes their block, uncaught.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Il distruttore può anche essere chiamato direttamente, ad esempio per distruggere un oggetto che è stato costruito utilizzando per posizionamento nuovo o tramite una funzione di allocatore membro come std :: allocatore :: destroy (), per distruggere un oggetto che è stato costruito con l'allocatore. Si noti che chiamare un distruttore direttamente per un oggetto comune, come ad esempio una variabile locale, richiama un comportamento indefinito, quando il distruttore viene chiamato di nuovo, alla fine del campo di applicazione.
Original:
The destructor may also be called directly, e.g. to destroy an object that was constructed using placement-new or through an allocator member function such as std :: allocatore :: destroy (), to destroy an object that was constructed through the allocator. Note that calling a destructor directly for an ordinary object, such as a local variable, invokes undefined behavior when the destructor is called again, at the end of scope.
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.
[modifica]Implicitamente-ha dichiarato distruttore
Se non definito dall'utente distruttore è previsto un tipo di classe (struct, class o union), il compilatore sempre dichiarare un distruttore come membro
inline public
della sua classe. Original:
If no user-defined destructor is provided for a class type (struct, class, or union), the compiler will always declare a destructor as an
inline public
member of its class. 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.
[modifica]Soppresso distruttore copia implicitamente dichiarato
Il distruttore implicitamente dichiarate o inadempiente per
T
classe è (fino al c++11) undefined / definito come cancellati(dal C++11) se una delle seguenti condizioni:Original:
The implicitly-declared or defaulted destructor for class
T
is undefined (fino al c++11) / defined as deleted(dal C++11) if any of the following is true: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.
T
ha un membro non statico di dati che non possono essere distrutti (ha eliminato o distruttore inaccessibili)Original:T
has a non-static data member that cannot be destructed (has deleted or inaccessible destructor)The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.T
ha classe base diretta o virtuale che non può essere distrutto (ha eliminato o distruttori inaccessibili)Original:T
has direct or virtual base class that cannot be destructed (has deleted or inaccessible destructors)The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.T
è un sindacato e ha un membro variante con non banale (dal C++11) distruttoreOriginal:T
is a union and has a variant member with non-trivial destructor (dal C++11)The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.- Il distruttore implicitamente dichiarato è virtuale (perché la classe base ha un distruttore virtuale) e la ricerca per la funzione di deallocazione (operator delete() traduce in una chiamata a ambigua, eliminate o funzioni inaccessibili.Original:The implicitly-declared destructor is virtual (because the base class has a virtual destructor) and the lookup for the deallocation function (operator delete() results in a call to ambiguous, deleted, or inaccessible function.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
[modifica]Distruttore Trivial
Il distruttore implicitamente dichiarato per la classe
T
è banale se tutte le seguenti condizioni:Original:
The implicitly-declared destructor for class
T
is trivial if all of the following is true: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.
- Il distruttore non è virtuale (vale a dire, il distruttore della classe base non è virtuale)Original:The destructor is not virtual (that is, the base class destructor is not virtual)The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - Tutte le classi base dirette hanno distruttori virtualiOriginal:All direct base classes have virtual destructorsThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - Tutti i non-membri dati statici di..... tipo di classe (o una matrice di tipo di classe) hanno distruttori virtualiOriginal:All non-static data members of class type (or array of class type) have virtual destructorsThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Un distruttore banale è un distruttore che non esegue alcuna azione. oggetti con distruttori banali non hanno bisogno di un delete-espressione e possono essere eliminati semplicemente deallocazione loro conservazione. Tutti tipi di dati compatibili con il linguaggio C (tipo POD) sono banalmente distruttibili.
Original:
A trivial destructor is a destructor that performs no action. Objects with trivial destructors don't require a delete-expression and may be disposed of by simply deallocating their storage. All data types compatible with the C language (POD types) are trivially destructible.
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.
[modifica]Implicitamente definito distruttore
Se il distruttore implicitamente dichiarato-non viene eliminato o banale, è definito (cioè corpo di una funzione viene generato e compilato) da parte del compilatore. Questo distruttore implicitamente definito ha un corpo vuoto.
Original:
If the implicitly-declared destructor is not deleted or trivial, it is defined (that is, a function body is generated and compiled) by the compiler. This implicitly-defined destructor has an empty body.
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.
[modifica]Distruzione sequenza
Per entrambi i distruttori definiti dall'utente o implicitamente definito, dopo che il corpo del distruttore viene eseguito, il compilatore chiama i distruttori per tutti i non-statici non variante membri della classe, in ordine inverso di dichiarazione, poi chiama i distruttori di tutte le classi di base diretti in ordine inverso di costruzione (che a sua volta chiamare i distruttori dei loro membri e delle loro classi base, ecc), e poi, se questo oggetto è di più derivati classe, chiama i distruttori di tutte le basi virtuali.
Original:
For both user-defined or implicitly-defined destructors, after the body of the destructor is executed, the compiler calls the destructors for all non-static non-variant members of the class, in reverse order of declaration, then it calls the destructors of all direct base classes in reverse order of construction (which in turn call the destructors of their members and their base classes, etc), and then, if this object is of most-derived class, it calls the destructors of all virtual bases.
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.
Anche quando il distruttore viene chiamato direttamente (ad esempio obj.~Foo();), l'istruzione return nel ~Foo() non restituisce il controllo al chiamante immediatamente:. chiama tutti quei distruttori membri e prima base..
Original:
Even when the destructor is called directly (e.g. obj.~Foo();), the return statement in ~Foo() does not return control to the caller immediately: it calls all those member and base destructors first.
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.
[modifica]Distruttori virtuali
Eliminazione di un oggetto attraverso il puntatore alla base richiama un comportamento indefinito a meno che il distruttore della classe base è virtuale:
Original:
Deleting an object through pointer to base invokes undefined behavior unless the destructor in the base class is virtual:
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.
class Base {
public:
virtual ~Base(){}
};
class Derived :public Base {};
Base* b = new Derived;
delete b;// safe
Una linea guida comune è che un distruttore per una classe base deve essere either public and virtual or protected and nonvirtual
Original:
A common guideline is that a destructor for a base class must be either public and virtual or protected and nonvirtual
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.
[modifica]Pure distruttori virtuali
Un distruttore può essere dichiarato puro virtuale... , per esempio in una classe di base che deve essere resa astratta, ma non ha altre funzioni che potrebbero essere adatti dichiarati virtuale pura destructor Tale deve avere una definizione, poiché tutti i distruttori classe base vengono sempre chiamati quando la classe derivata viene distrutto.:
Original:
A destructor may be declared pure virtual, for example in a base class which needs to be made abstract, but has no other suitable functions that could be declared pure virtual. Such destructor must have a definition, since all base class destructors are always called when the derived class is destroyed:
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.
class AbstractBase {
public:
virtual ~AbstractBase()=0;
};
AbstractBase::~AbstractBase(){}
class Derived :public AbstractBase {};
// AbstractBase obj; // compiler error
Derived obj; // OK