◐ Shell
clean mode source ↗

Copy constructors – cppreference.com

Aus cppreference.com

<metanoindex/>

Eine Kopie Konstruktor der Klasse T ist eine Non-template Konstruktor, dessen erste Parameter ist T&, const T&, volatile T& oder const volatile T&, und entweder gibt es keine weiteren Parameter oder der Rest der alle Parameter Standardwerte haben. Ein Typ mit einem öffentlichen Copy-Konstruktor ist CopyConstructible .

Original:

A copy constructor of class T is a non-template constructor whose first parameter is T&, const T&, volatile T&, or const volatile T&, and either there are no other parameters, or the rest of the parameters all have default values. A type with a public copy constructor is CopyConstructible.

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Syntax

class_name ( const class_name & ) (1)
class_name ( const class_name & ) = default; (1)
class_name ( const class_name & ) = delete; (1)

Erklärung

# Typische Deklaration einer Copy-Konstruktor

Original:

# Typical declaration of a copy constructor

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

# Erzwingen eine Kopie Konstruktor vom Compiler erzeugt werden

Original:

# Forcing a copy constructor 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.

# Vermeiden impliziten Standardkonstruktor

Original:

# Avoiding implicit default constructor

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Der Copy-Konstruktor wird aufgerufen, wenn ein Objekt von einem anderen Objekt des gleichen Typs, die beinhaltet initialisiert wird

Original:

The copy constructor is called whenever an object is initialized from another object of the same type, which includes

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

  • Initialisierung T a = b; oder T a(b);, wo b vom Typ T ist

    Original:

    initialization, T a = b; or T a(b);, where b is of type T

    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.

  • Funktion Argumentübergabe: f(a);, wo a der Typ ist T und f ist void f(T t)

    Original:

    function argument passing: f(a);, where a is of type T and f is void f(T t)

    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.

  • Return: return a; innerhalb einer Funktion wie T f(), wo a ist vom Typ T, die keine Anstalten Konstruktor hat .

    Original:

    function return: return a; inside a function such as T f(), where a is of type T, which has no move constructor.

    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.

Implizit deklarierte Copy-Konstruktor

Wenn keine benutzerdefinierten Kopie Konstruktoren für eine Klasse-Typ (struct, class oder union) vorgesehen sind, wird der Compiler immer erklären, eine Kopie Konstruktor als inline public Mitglied seiner Klasse. Diese implizit deklariert Copykonstruktor hat die Form T::T(const T&), wenn alle der folgenden Bedingungen erfüllt ist:

Original:

If no user-defined copy constructors are provided for a class type (struct, class, or union), the compiler will always declare a copy constructor as an inline public member of its class. This implicitly-declared copy constructor has the form T::T(const T&) 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.

  • alle direkten und virtuellen Basen der T haben Kopie Konstruktoren mit Verweisen auf const oder const volatile als ersten Parameter

    Original:

    all direct and virtual bases of T have copy constructors with references to const or to const volatile as their first parameters

    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 Member T haben Kopie Konstruktoren mit Verweisen auf const oder const volatile als ersten Parameter

    Original:

    all non-static members of T have copy constructors with references to const or to const volatile as their first parameters

    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.

Ansonsten ist die implizit deklariert Copykonstruktor T::T(T&). (Beachten Sie, dass aufgrund dieser Regeln, die implizit deklariert Copykonstruktor kann nicht auf einen flüchtigen lvalue Argument binden)

Original:

Otherwise, the implicitly-declared copy constructor is T::T(T&). (Note that due to these rules, the implicitly-declared copy constructor cannot bind to a volatile lvalue argument)

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Eine Klasse kann mehrere Kopie Konstruktoren, z. B. sowohl T::T(const T&) und T::T(T&). Wenn einige benutzerdefinierte Kopie Konstruktoren vorhanden sind, kann der Benutzer noch zwingen die Erzeugung des implizit deklarierten Copykonstruktor mit dem Schlüsselwort default (seit C++11) .

Original:

A class can have multiple copy constructors, e.g. both T::T(const T&) and T::T(T&). If some user-defined copy constructors are present, the user may still force the generation of the implicitly declared copy 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.

Gelöschte implizit deklariert Copy-Konstruktor

Die implizit deklariert oder ausgefallen Kopie Konstruktor für die Klasse T ist undefiniert (bis C + +11) / definiert als gelöscht (seit C++11) in einem der folgenden Punkte zutrifft:

Original:

The implicitly-declared or defaulted copy constructor for class T is undefined (bis C + +11) / defined as deleted (seit C++11) in 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 nicht-statische Datenelemente, die nicht kopiert werden (gelöscht haben, unzugänglich oder zweideutige Kopie Konstruktoren) werden können

    Original:

    T has non-static data members that cannot be copied (have deleted, inaccessible, or ambiguous copy constructors)

    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 kopiert werden können (gelöscht hat, unzugänglich oder mehrdeutig Kopie Konstruktoren) werden

    Original:

    T has direct or virtual base class that cannot be copied (has deleted, inaccessible, or ambiguous copy constructors)

    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 mit einer gelöschten oder unzugänglichen destructor

    Original:

    T has direct or virtual base class with a 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 eine benutzerdefinierte move Konstruktor oder unterwegs Zuweisungsoperator (seit C++11)

    Original:

    T has a user-defined move constructor or move assignment operator (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 ist ein Zusammenschluss und hat eine Variante Mitglied nicht-triviale Copykonstruktor (seit C++11)

    Original:

    T is a union and has a variant member with non-trivial copy 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 hat eine Daten Mitglied rvalue Referenztyp (seit C++11)

    Original:

    T has a data member of rvalue reference type (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.

Trivial Copy-Konstruktor

Die implizit deklariert Kopie Konstruktor für die Klasse T ist trivial, wenn alle der folgenden Bedingungen erfüllt ist:

Original:

The implicitly-declared copy 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.

  • Der Copy-Konstruktor für jede direkte Basis von T gewählt ist trivial

    Original:

    The copy constructor selected for every direct base of T is trivial

    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.

  • Der Copy-Konstruktor für jedes nicht statische Klasse-Typ (oder ein Array von Klasse-Typ) memeber der T gewählt ist trivial

    Original:

    The copy constructor selected for every non-static class type (or array of class type) memeber of T is trivial

    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.

Eine triviale Copy-Konstruktor ist ein Konstruktor, der eine byteweise Kopie des Objekts Darstellung der Argumente wird, und führt keine weitere Aktion. Objekte mit trivialen Kopie Konstruktoren kann durch Kopieren Gegenstand Darstellungen manuell kopiert werden, zB mit std::memmove. Alle Datentypen kompatibel mit der Programmiersprache C (POD-Typen) sind trivial kopierbar .

Original:

A trivial copy constructor is a constructor that creates a bytewise copy of the object representation of the argument, and performs no other action. Objects with trivial copy constructors can be copied by copying their object representations manually, e.g. with std::memmove. All data types compatible with the C language (POD types) are trivially copyable.

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Copy-Konstruktor implizit definiert

Wenn die implizit deklariert Kopierkonstruktor nicht gelöscht oder trivial, ist es definiert (das heißt, eine Funktion Körper erzeugt und kompiliert) durch den Compiler. Für union Typen, kopiert das implizit definierten Copykonstruktor die Objekt-Repräsentation (wie std::memmove). Für Nicht-union-Klasse-Typen (class und struct), führt der Konstruktor Vollmitglied-weise Kopie des Objekts Basen und nicht-statische Mitglieder, in deren Initialisierung Reihenfolge mit direkte Initialisierung .

Original:

If the implicitly-declared copy constructor is not deleted or trivial, it is defined (that is, a function body is generated and compiled) by the compiler. For union types, the implicitly-defined copy constructor copies the object representation (as by std::memmove). For non-union class types (class and struct), the constructor performs full member-wise copy of the object's bases and non-static members, in their initialization order, using direct initialization.

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Die Erzeugung der implizit definierten Copy-Konstruktor ist deprecated(seit C++11) wenn T hat einen benutzerdefinierten Destruktor oder benutzerdefinierten Zuweisungsoperator .

Original:

The generation of the implicitly-defined copy constructor is deprecated(seit C++11) if T has a user-defined destructor or user-defined copy assignment operator.

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Notes

In vielen Situationen Kopierkonstruktoren durchgeführt werden, selbst wenn sie beobachtbaren Nebenwirkungen erzeugen würde optimiert finden Kopie elision

Original:

In many situations, copy constructors are optimized out even if they would produce observable side-effects, see Kopie elision

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Beispiel

struct A {
    int n;
    A(int n=1) : n(n) {}
    A(const A& a) : n(a.n) {} // user-defined copy ctor
};

struct B : A {
    // implicit default ctor B::B()
    // implicit copy ctor B::B(const B&) 
};

struct C : B {
     C() : B() {}
 private:
     C(const C&); // non-copiable, C++98 style
};

int main()
{
    A a1(7);
    A a2(a1); // calls the copy ctor
    B b;
    B b2 = b;
    A a3 = b; // conversion to A& and copy ctor
    volatile A va(10);
    // A a4 = va; // compile error

    C c;
    // C c2 = c; // compile error
}