Namensräume
Varianten
Aktionen

Default constructors

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.
Standardkonstruktor
Copy-Konstruktor
bewegen Konstruktor (C++11)
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 Standardkonstruktor ist ein Konstruktor, der ohne Argumente (entweder mit einer leeren Parameterliste definiert, oder mit Standard-Argumente für jeden Parameter zur Verfügung gestellt) aufgerufen werden kann. Ein Typ mit einem öffentlichen Standardkonstruktor ist DefaultConstructible .
Original:
A default constructor is a constructor which can be called with no arguments (either defined with an empty parameter list, or with default arguments provided for every parameter). A type with a public default constructor is DefaultConstructible.
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

ClassName ( ) ; (1)
ClassName :: ClassName ( ) body (2)
ClassName() = delete ; (3) (seit C++11)
ClassName() = default ; (4) (seit C++11)

[Bearbeiten] Erklärung

1)
Erklärung eines Standardkonstruktor
Original:
Declaration of a default constructor
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
2)
Definition des Konstruktors außerhalb der Klasse Körper
Original:
Definition of the constructor outside the class body
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
3)
Die Hemmung der automatischen Generierung eines Standardkonstruktor
Original:
Inhibiting the automatic generation of a default constructor
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
4)
Explizit zwingt die automatische Generierung eines Standardkonstruktor
Original:
Explicitly forcing the automatic generation of a default constructor
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
ClassName ist die Kennung des umschließenden Klasse
Original:
ClassName is the identifier of the enclosing class
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Die Standard-Konstruktoren werden aufgerufen, wenn:
Original:
The default constructors are called when:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
  • Erstellen von Objekten oder Arrays von statischen, thread-lokalen, sowie automatische Speicherung Dauer, ohne Initialisierung deklariert werden, (T obj; oder T arr[10];)
    Original:
    creating objects or arrays of static, thread-local, and automatic storage duration that are declared without an initializer, (T obj; or T arr[10];)
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • Erstellen von Objekten der dynamischen Lagerdauer, wenn der neue Ausdruck ohne Initialisierung (new T;) geschrieben wird
    Original:
    creating objects of dynamic storage duration when the new-expression is written without an initializer (new T;)
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • Erstellen von Arrays dynamischer Lagerdauer mit dem Ausdruck new T[n]
    Original:
    creating arrays of dynamic storage duration with the expression new T[n]
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • Wertschöpfung initialisiert temporäre Objekte mit dem Cast-Ausdruck T() .
    Original:
    creating value-initialized temporary objects with the cast expression T().
    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 Standardkonstruktor

Wenn keine benutzerdefinierte Konstruktoren jeglicher Art für eine Klasse-Typ (struct, class oder union) vorgesehen sind, wird der Compiler immer erklären, einen Standardkonstruktor als inline public Mitglied seiner Klasse. Wenn einige benutzerdefinierte Konstruktoren vorhanden sind, kann der Benutzer noch zwingen die Erzeugung des implizit deklarierten Konstruktor mit dem Schlüsselwort default (seit C++11) .
Original:
If no user-defined constructors of any kind are provided for a class type (struct, class, or union), the compiler will always declare a default constructor as an inline public member of its class. If some user-defined constructors are present, the user may still force the generation of the implicitly declared constructor with the keyword default (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.

[Bearbeiten] Gelöschte implizit deklariert Standardkonstruktor

Die implizit deklariert oder ausgefallen Standardkonstruktor 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 default constructor 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 ein Mitglied des Referenz-Typ (ohne Klammer-oder-gleich initializer(seit C++11)) .
    Original:
    T has a member of reference type (without a brace-or-equal initializer(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.
  • T eine const Mitglied (ohne Klammer-oder-gleich initializer(seit C++11)) oder eine benutzerdefinierte Standardkonstruktor .
    Original:
    T has a const member (without a brace-or-equal initializer(seit C++11)) or a user-defined default constructor.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • T ein Element (ohne Klammer-oder-gleich initializer(seit C++11)), die eine gelöschte Standardkonstruktor hat, oder dessen Standardkonstruktor ist mehrdeutig oder unzugänglich von diesem Konstruktor .
    Original:
    T has a member (without a brace-or-equal initializer(seit C++11)), which has a deleted default constructor, or its default constructor is ambiguous or inaccessible from this constructor.
    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 direkte oder virtuelle Basis, die eine gelöschte Standardkonstruktor verfügt, oder es ist zweideutig oder unzugänglich von diesem Konstruktor .
    Original:
    T has a direct or virtual base which has a deleted default constructor, or it is ambiguous or inaccessible from this constructor.
    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 direkte oder virtuelle Basis, die eine gelöschte Destruktor oder einen Destruktor unzugänglichen von diesem Konstruktor hat .
    Original:
    T has a direct or virtual base which has a deleted destructor, or a destructor that is inaccessible from this constructor.
    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 union mit mindestens einer Variante Element mit nicht-trivialen Standardwert constructor(seit C++11) .
    Original:
    T is a union with at least one variant member with non-trivial default constructor(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.
  • T wird ein union und alle seine Variante Mitglieder sind const .
    Original:
    T is a union and all of its variant members are const.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.

[Bearbeiten] Trivial Standardkonstruktor

Die implizit deklariert Standardkonstruktor für die Klasse T ist trivial, wenn alle der folgenden Bedingungen erfüllt ist:
Original:
The implicitly-declared default constructor 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.
  • T hat keine virtuelle Member-Funktionen
    Original:
    T has no virtual member functions
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • T hat keine virtuellen Basisklassen
    Original:
    T has no virtual base classes
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • T hat keine nicht-statische Member mit Strebe-oder-gleich initializers (seit C++11)
    Original:
    T has no non-static members with brace-or-equal initializers (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.
  • Jede direkte Basis T hat einen trivialen Standardkonstruktor
    Original:
    Every direct base of T has a trivial default constructor
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • Jede nicht-statische Member der Klasse Typ hat einen trivialen Standardkonstruktor
    Original:
    Every non-static member of class type has a trivial default constructor
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
Eine triviale Standardkonstruktor ist ein Konstruktor, der keine Aktion ausführt. Objekte mit trivialen Standardkonstruktor Konstruktoren können mit reinterpret_cast auf jedem Speicher erstellt werden, z. B. am Speicher mit std::malloc zugeordnet. Alle Datentypen kompatibel mit der Programmiersprache C (POD-Typen) sind trivial default-konstruierbar .
Original:
A trivial default constructor is a constructor that performs no action. Objects with trivial default constructors can be created by using reinterpret_cast on any storage, e.g. on memory allocated with std::malloc. All data types compatible with the C language (POD types) are trivially default-constructible.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[Bearbeiten] Standardkonstruktor implizit definiert

Wenn die implizit deklariert Standardkonstruktor nicht gelöscht oder trivial, ist es definiert (das heißt, eine Funktion Körper erzeugt und kompiliert) durch den Compiler, und es hat genau die gleiche Wirkung wie eine benutzerdefinierte Konstruktor mit leeren Körper und leer Initialisierungsliste. Das heißt, nennt es die Standard-Konstruktoren der Basen und der nicht-statische Member dieser Klasse .
Original:
If the implicitly-declared default constructor is not deleted or trivial, it is defined (that is, a function body is generated and compiled) by the compiler, and it has exactly the same effect as a user-defined constructor with empty body and empty initializer list. That is, it calls the default constructors of the bases and of the non-static members of this class.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[Bearbeiten] Beispiel

struct A {
    int x;
    A(int x = 1) : x(x) {} // user-defined default ctor
};
struct B : A {
    // B::B() is implicitly-defined, calls A::A()
};
struct C {
    A obj;
    // C::C() is implicitly-defined, calls A::A()
};
struct D : A {
    D(int y) : A(y) {}
    // D::D() is not declared because another constructor exists
};
struct E : A
{
    E(int y) : A(y) {}
    E() = default; // explicitly defaulted, calls A::A()
};
 
struct F {
    int& ref; // reference member
    const int c; // const member
    // Bad::Bad() is deleted
};
 
int main()
{
    A a;
    B b;
//  D d; // compile error
    E e;
//  F f; // compile error
}