Namensräume
Varianten
Aktionen

Destructors

Aus cppreference.com
< cpp‎ | language

 
 
Sprache C + +
Allgemeine Themen
Original:
General topics
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Flusskontrolle
Original:
Flow control
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Bedingte Ausführung Aussagen
Original:
Conditional execution statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Iterationsanweisungen
Original:
Iteration statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Gehe Aussagen
Original:
Jump statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Funktionen
Original:
Functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Funktion Erklärung
Lambda-Funktion Erklärung
Funktions-Template
inline-Spezifizierer
Exception-Spezifikationen (veraltet)
noexcept Spezifizierer (C++11)
Ausnahmen
Original:
Exceptions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Namespaces
Original:
Namespaces
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Types
Original:
Types
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
decltype specifier (C++11)
Specifiers
Original:
Specifiers
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
cv Planer
Lagerdauer Planer
constexpr Spezifizierer (C++11)
auto Spezifizierer (C++11)
alignas Spezifizierer (C++11)
Initialisierung
Original:
Initialization
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Literale
Original:
Literals
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Expressions
Original:
Expressions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
alternative Darstellungen
Utilities
Original:
Utilities
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Types
Original:
Types
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
typedef declaration
Typ Aliasdeklaration (C++11)
Attribute (C++11)
Wirft
Original:
Casts
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
impliziten Konvertierungen
const_cast conversion
static_cast conversion
dynamic_cast conversion
reinterpret_cast conversion
C-Stil und funktionale Besetzung
Speicherzuweisung
Original:
Memory allocation
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Classes
Original:
Classes
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Class-spezifische Funktion Eigenschaften
Original:
Class-specific function properties
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
explizit (C++11)
statisch
Besondere Member-Funktionen
Original:
Special member functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Templates
Original:
Templates
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Klassen-Template
Funktions-Template
Template-Spezialisierung
Parameter Packs (C++11)
Verschiedenes
Original:
Miscellaneous
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Inline Montage
 
Ein Destruktor ist eine spezielle Member-Funktion, die aufgerufen wird, wenn die Lebensdauer eines Objekts endet wird. Der Zweck des destructor ist es, die Ressourcen, die das Objekt während seiner Lebenszeit erworben hat befreien .
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.

Inhaltsverzeichnis

[Bearbeiten] Syntax

~class_name (); (1)
virtual ~class_name (); (2)
~class_name () = default; (3) (seit C++11)
~class_name () = delete; (4) (seit C++11)

[Bearbeiten] Erklärung

# Typische Deklaration einer destructor
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.
# Virtual destructor ist in der Regel in einer Basisklasse benötigt
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.
# Erzwingen ein Destruktor vom Compiler erzeugt werden
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.
# Deaktivieren der impliziten Destruktor
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.
Der Destruktor aufgerufen wird, wenn ein Objekt die Lebensdauer endet, umfasst die
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.
  • Beendigung des Programms, für Objekte mit statischen Lagerdauer
    Original:
    program termination, for objects with static storage duration
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • Thema Ausfahrt für Objekte mit lokalen Thread-Lagerdauer (seit C++11)
    Original:
    thread exit, for objects with thread-local storage duration (seit C++11)
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • Ende Umfang, für Objekte mit dynamischem Speicher Dauer und für Provisorien dessen Leben durch Bindung an eine Referenz verlängert
    Original:
    end of scope, for objects with automatic storage duration and for temporaries whose life was extended by binding to a reference
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • delete-Ausdruck für Objekte mit dynamischen Lagerdauer
    Original:
    delete-expression, for objects with dynamic storage duration
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • Ende des vollen Ausdruck, für namenlose Provisorien
    Original:
    end of the full expression, for nameless temporaries
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • Stack Abwickeln, für Objekte mit automatischer Lagerdauer, wenn eine Ausnahme entgeht ihrem Block, abgefangene .
    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.
Der Destruktor kann auch direkt aufgerufen werden, z. B. ein Objekt, das unter Verwendung Placement-new oder durch eine Zuweisung Member-Funktion, wie std :: allocator :: destroy (), um ein Objekt, das durch die Zuweisung gebaut wurde zerstört wurde zerstören. Beachten Sie, dass der Aufruf einen Destruktor direkt für ein gewöhnliches Objekt, wie eine lokale Variable, ruft undefinierten Verhalten, wenn der Destruktor erneut aufgerufen wird, am Ende des Umfangs .
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 :: allocator :: 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.

[Bearbeiten] Implizit deklarierte Destruktor

Wenn keine benutzerdefinierten Destruktor für eine Klasse-Typ (struct, class oder union) vorgesehen ist, wird der Compiler immer erklären, einen Destruktor als inline public Mitglied seiner Klasse .
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.

[Bearbeiten] Gelöschte implizit deklariert Kopie destructor

Die implizit deklariert oder ausgefallen Destruktor für die Klasse T ist undefiniert (bis C + +11) / definiert als gelöscht (seit C++11), wenn einer der folgenden Punkte zutrifft:
Original:
The implicitly-declared or defaulted destructor for class T is undefined (bis C + +11) / defined as deleted (seit 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.
  • T hat eine nicht-statische Daten Mitglied, die nicht zu zerstört (gelöscht oder unzugänglich destructor)
    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 unmittelbar oder virtuelle Basisklasse, die nicht zu zerstört (gelöscht oder unzugänglich Destruktoren)
    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 ist ein Zusammenschluss und hat eine Variante Mitglied mit nicht-trivialen Destruktor (seit C++11)
    Original:
    T is a union and has a variant member with non-trivial destructor (seit C++11)
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • Die implizit deklariert Destruktor virtuell ist (weil die Basisklasse hat einen virtuellen Destruktor) und die Suche nach dem deallocation Funktion (operator delete() Ergebnisse in einem Aufruf mehrdeutig, gelöscht oder unzugänglich Funktion .
    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.

[Bearbeiten] Trivial destructor

Die implizit deklariert Destruktor für die Klasse T ist trivial, wenn alle der folgenden Bedingungen erfüllt ist:
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.
  • Der Destruktor nicht virtuell ist (das heißt, ist die Basisklasse Destruktor nicht virtuell)
    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.
  • Alle direkten Basisklassen haben virtuelle Destruktoren
    Original:
    All direct base classes have virtual destructors
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • Alle nicht-statische Daten Mitglieder..... Klasse Typ (oder ein Array von Klasse-Typ) haben virtuelle Destruktoren
    Original:
    All non-static data members of class type (or array of class type) have virtual destructors
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
Eine triviale destructor ist ein Destruktor, der keine Aktion ausführt. Objekte mit trivialen Destruktoren benötigen keine delete-Ausdruck und kann aus durch einfaches Freigeben deren Lagerung entsorgt werden. Alle Datentypen kompatibel mit der Sprache C (POD-Typen) sind trivial zerstörbare .
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.

[Bearbeiten] Destruktor implizit definiert

Wenn die implizit deklariert destructor nicht gelöscht wird oder trivial, sie definiert ist (das heißt, eine Funktion Körpers erzeugt und kompiliert) vom Compiler. Diese implizit definierte Destruktor hat eine leere Körper .
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.

[Bearbeiten] Zerstörung Sequenz

Für beide benutzerdefinierten oder implizit definierte Destruktoren, nachdem der Körper der Destruktor ausgeführt wird, ruft der Compiler die Destruktoren für alle nicht-statische Nicht-Varianten Mitglieder der Klasse, in umgekehrter Reihenfolge der Deklaration, dann ruft die Destruktoren aller direkten Basisklassen in umgekehrter Reihenfolge der Konstruktion (was wiederum nennen die Destruktoren ihrer Mitglieder und ihrer Basisklassen, etc), und dann, wenn dieses Objekt ist von den meisten abgeleitete Klasse, ruft es die Destruktoren aller virtuellen Basen .
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.
Selbst wenn der Destruktor direkt aufgerufen wird (zB obj.~Foo();), der return-Anweisung in ~Foo() nicht die Steuerung wieder an den Aufrufer sofort:. es nennt all jene Mitgliedes und Basis Destruktoren erste. .
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.

[Bearbeiten] Virtuelle Destruktoren

Löschen eines Objekts durch Zeiger auf Basis aufruft undefinierten Verhalten, wenn der Destruktor in der Basisklasse virtuell ist:
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.

class Base {
 public:
    virtual ~Base() {}
};
class Derived : public Base {};
Base* b = new Derived;
delete b; // safe

Eine gemeinsame Richtlinie ist, dass ein Destruktor für eine Basisklasse muss either public and virtual or protected and nonvirtual sein
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.

[Bearbeiten] Rein virtuelle Destruktoren

Ein Destruktor deklariert werden rein virtuelle werden... zum Beispiel in einer Basisklasse zu machende abstrakten werden muss, hat aber keine andere geeignete Funktionen, die rein virtuelle deklariert werden könnten solche destructor muss eine Definition haben, da alle Basisklasse Destruktoren immer werden aufgerufen, wenn die abgeleitete Klasse zerstört wird.:
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.

class AbstractBase {
 public:
    virtual ~AbstractBase() = 0;
};
AbstractBase::~AbstractBase() {}
class Derived : public AbstractBase {};
// AbstractBase obj;   // compiler error
Derived obj;           // OK

[Bearbeiten] Beispiel

#include <iostream>
 
struct A
{
    int i;
 
    A ( int i ) : i ( i ) {}
 
    ~A()
    {
        std::cout << "~a" << i << std::endl;
    }
};
 
int main()
{
    A a1(1);
    A* p;
 
    { // nested scope
        A a2(2);
        p = new A(3);
    } // a2 out of scope
 
    delete p; // calls the destructor of a3
}

Output:

~a2
~a3
~a1