Namensräume
Varianten
Aktionen

Copy assignment operator

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.
Kopieren Zuordnung
bewegen Zuordnung (C++11)
Destruktor
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
 
Eine Kopie Zuweisungsoperator der Klasse T ist eine Non-Vorlage nicht-statische Member-Funktion mit dem Namen operator=, die genau einen Parameter vom Typ T, T&, const T&, volatile T& oder const volatile T& dauert. Ein Typ mit einer öffentlichen Kopie Zuweisungsoperator ist CopyAssignable .
Original:
A copy assignment operator of class T is a non-template non-static member function with the name operator= that takes exactly one parameter of type T, T&, const T&, volatile T&, or const volatile T&. A type with a public copy assignment operator is CopyAssignable.
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 & class_name :: operator= ( class_name ) (1) (seit C++11)
class_name & class_name :: operator= ( const class_name & ) (2) (seit C++11)
class_name & class_name :: operator= ( const class_name & ) = default; (3) (seit C++11)
class_name & class_name :: operator= ( const class_name & ) = delete; (4) (seit C++11)

[Bearbeiten] Erklärung

# Typische Deklaration einer Zuweisungsoperator, wenn copy-and-swap idiom verwendet werden können
Original:
# Typical declaration of a copy assignment operator when copy-and-swap idiom can be used
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
# Typische Deklaration einer Zuweisungsoperator bei copy-and-Swap-Idiom nicht verwendet werden kann
Original:
# Typical declaration of a copy assignment operator when copy-and-swap idiom cannot be used
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
# Erzwingen einer Zuweisungsoperator vom Compiler erzeugt werden
Original:
# Forcing a copy assignment operator 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 Zuweisungsoperator
Original:
# Avoiding implicit copy assignment
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Der Zuweisungsoperator wird aufgerufen, wenn durch Überlast Auflösung, zB ausgewählte wenn ein Objekt auf der linken Seite einer Zuweisung Ausdruck .
Original:
The copy assignment operator is called whenever selected by Überlast Auflösung, e.g. when an object appears on the left side of an assignment expression.
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 Zuweisungsoperator

Wenn keine benutzerdefinierten Kopie Zuweisungsoperatoren für eine Klasse-Typ (struct, class oder union) vorgesehen sind, wird der Compiler immer erklären, ein als Inline öffentlichen Member der Klasse. Diese implizit deklariert Zuweisungsoperator hat die Form T& T::operator=(const T&), wenn alle der folgenden Bedingungen erfüllt ist:
Original:
If no user-defined copy assignment operators are provided for a class type (struct, class, or union), the compiler will always declare one as an inline public member of the class. This implicitly-declared copy assignment operator has the form T& T::operator=(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.
  • Jede direkte Basis B der T hat einen Zuweisungsoperator, deren Parameter B oder const B& oder const volatile B&
    Original:
    each direct base B of T has a copy assignment operator whose parameters are B or const B& or const volatile B&
    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 Daten Mitglied M der T der Klasse-Typ oder ein Array von Klasse-Typ hat eine Kopie Zuweisungsoperator deren Parameter M oder const M& oder const volatile M&
    Original:
    each non-static data member M of T of class type or array of class type has a copy assignment operator whose parameters are M or const M& or const volatile M&
    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 Zuweisungsoperator als T& T::operator=(T&) erklärt. (Beachten Sie, dass aufgrund dieser Regeln, die implizit deklariert Zuweisungsoperator kann nicht auf einen flüchtigen lvalue Argument binden)
Original:
Otherwise the implicitly-declared copy assignment operator is declared as T& T::operator=(T&). (Note that due to these rules, the implicitly-declared copy assignment operator 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 Kopien Zuweisungsoperatoren, zB sowohl T& T::operator=(const T&) und T& T::operator=(T). Wenn einige benutzerdefinierte Kopie Zuweisungsoperatoren vorhanden sind, kann der Benutzer noch zwingen die Erzeugung des implizit deklarierten Zuweisungsoperator mit dem Schlüsselwort default .
Original:
A class can have multiple copy assignment operators, e.g. both T& T::operator=(const T&) and T& T::operator=(T). If some user-defined copy assignment operators are present, the user may still force the generation of the implicitly declared copy assignment operator with the keyword default.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Da der Zuweisungsoperator ist immer für jede Klasse deklariert wird, wird die Basisklasse Zuweisungsoperator immer versteckt. Wenn eine using-Deklaration wird verwendet, um in der Zuweisungsoperator von der Basisklasse zu bringen, und das Argument type könnte das gleiche wie das Argument Typ des impliziten Zuweisungsoperator der abgeleiteten Klasse zu sein, wird die using-Deklaration auch von der impliziten versteckt Erklärung .
Original:
Because the copy assignment operator is always declared for any class, the base class assignment operator is always hidden. If a using-declaration is used to bring in the assignment operator from the base class, and its argument type could be the same as the argument type of the implicit assignment operator of the derived class, the using-declaration is also hidden by the implicit declaration.
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 Zuweisungsoperator

Die implizit deklariert oder ausgefallen Zuweisungsoperator für die Klasse T ist definiert als gelöscht in einer der folgenden Punkte zutrifft:
Original:
The implicitly-declared or defaulted copy assignment operator for class T is defined as deleted 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 einen nicht-statisches Datenelement, das const ist
    Original:
    T has a non-static data member that is const
    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 einer Referenz-Typ .
    Original:
    T has a non-static data member of a reference type.
    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 kopiergeschützt zugeordnet werden können (gelöscht hat, unzugänglich oder mehrdeutig Zuweisungsoperator)
    Original:
    T has a non-static data member that cannot be copy-assigned (has deleted, inaccessible, or ambiguous 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.
  • T hat direkte oder virtuelle Basisklasse, die nicht kopiergeschützt zugeordnet werden können (gelöscht hat, unzugänglich oder mehrdeutig move Zuweisungsoperator)
    Original:
    T has direct or virtual base class that cannot be copy-assigned (has deleted, inaccessible, or ambiguous move assignment operator)
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • T verfügt über eine benutzerfreundliche erklärt move Konstruktor
    Original:
    T has a user-declared move constructor
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • T verfügt über eine benutzerfreundliche erklärt move Zuweisungsoperator
    Original:
    T has a user-declared move assignment operator
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.

[Bearbeiten] Trivial Zuweisungsoperator

Die implizit deklariert Zuweisungsoperator für die Klasse T ist trivial, wenn alle der folgenden Bedingungen erfüllt ist:
Original:
The implicitly-declared copy assignment operator 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 Zuweisungsoperator für jede direkte Basis von T gewählt ist trivial
    Original:
    The copy assignment operator 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 Zuweisungsoperator für jeden nicht-statische Klasse-Typ (oder ein Array von Klasse-Typ) memeber der T gewählt ist trivial
    Original:
    The copy assignment operator 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 Zuweisungsoperator macht eine Kopie des Objekts Darstellung wie von std::memmove. Alle Datentypen kompatibel mit der Programmiersprache C (POD-Typen) sind trivial copy-zuweisbare .
Original:
A trivial copy assignment operator makes a copy of the object representation as if by std::memmove. All data types compatible with the C language (POD types) are trivially copy-assignable.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[Bearbeiten] Zuweisungsoperator implizit definiert

Wenn die implizit deklariert Zuweisungsoperator 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 Zuweisungsoperator die Objekt-Repräsentation (wie std::memmove). Für Nicht-union-Klasse-Typen (class und struct), führt der Bediener Mitglied-weise Kopie Zuordnung des Objekts Basen und nicht-statische Mitglieder, in deren Initialisierung Reihenfolge mit, Verwendung des eingebauten Zuordnung für die Skalare und Zuweisungsoperator für Klasse Typen .
Original:
If the implicitly-declared copy assignment operator 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 assignment copies the object representation (as by std::memmove). For non-union class types (class and struct), the operator performs member-wise copy assignment of the object's bases and non-static members, in their initialization order, using, using built-in assignment for the scalars and copy assignment operator for class types.
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 Zuweisungsoperator ist deprecated(seit C++11) wenn T verfügt über eine benutzerfreundliche erklärt destructor oder benutzerdefinierte erklärt Copykonstruktor .
Original:
The generation of the implicitly-defined copy assignment operator is deprecated(seit C++11) if T has a user-declared destructor or user-declared copy constructor.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[Bearbeiten] Notes

Wenn beide kopieren und verschieben Zuweisungsoperatoren vorgesehen sind, wählt Überladungsauflösung den Umzug Zuordnung, wenn das Argument ein rvalue (entweder prvalue wie eine namenlose temporäre oder xWert, wie das Ergebnis der std::move ), und wählt den Zuweisungsoperator, wenn das Argument lvalue (benannte Objekt oder eine Funktion / Betreiber wieder lvalue Referenz). Wenn nur die Kopie Zuordnung haben, wählen Sie alle Argumente Kategorien es (wie lange es dauert ihr Argument als Wert oder als Verweis auf const, da rvalues ​​const Referenzen binden können), was Zuweisungsoperator das Fallback für unterwegs Zuordnung beim Bewegen nicht verfügbar ist .
Original:
If both copy and move assignment operators are provided, overload resolution selects the move assignment if the argument is an rvalue (either prvalue such as a nameless temporary or xvalue such as the result of std::move), and selects the copy assignment if the argument is lvalue (named object or a function/operator returning lvalue reference). If only the copy assignment is provided, all argument categories select it (as long as it takes its argument by value or as reference to const, since rvalues can bind to const references), which makes copy assignment the fallback for move assignment, when move is unavailable.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[Bearbeiten] Kopieren und tauschen

Kopieren Zuweisungsoperator kann im Hinblick auf die Copy-Konstruktor, Destruktor und der Swap () Funktion ausgedrückt werden, wenn man zur Verfügung:
Original:
Copy assignment operator can be expressed in terms of copy constructor, destructor, and the swap() member function, if one is provided:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

T& T::operator=(T arg) { // copy/move constructor is called to construct arg
    swap(arg);    // resources exchanged between *this and arg
    return *this;
}  // destructor is called to release the resources formerly held by *this

Für Nicht-Werfen swap (), bietet diese Form starke Ausnahme Garantie. Für rvalue Argumente, diese Form ruft automatisch die Bewegung Konstruktor und wird manchmal auch als "übergeordnete Zuweisungsoperator" (wie in, sowohl kopieren und verschieben) bezeichnet .
Original:
For non-throwing swap(), this form provides starke Ausnahme Garantie. For rvalue arguments, this form automatically invokes the move constructor, and is sometimes referred to as "unifying assignment operator" (as in, both copy and move).
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[Bearbeiten] Beispiel

#include <iostream>
#include <memory>
struct A {
    int n;
    std::string s1;
    // user-defined copy assignment, copy-and-swap form
    A& operator=(A other) {
        std::cout << "copy assignment of A\n";
        std::swap(n, other.n);
        std::swap(s1, other.s1);
        return *this;
    }
};
 
struct B : A {
    std::string s2;
    // implicitly-defined copy assignment
};
 
struct C {
     std::unique_ptr<int[]> data;
     std::size_t size;
     // non-copy-and-swap assignment
     C& operator=(const C& other) {
         // check for self-assignment
         if(&other == this)
             return *this;
         // reuse storage when possible
         if(size != other.size)
             data.reset(new int[other.size]);
         std::copy(&other.data[0],
                   &other.data[0] + std::min(size, other.size),
                   &data[0]);
         return *this;
     }
     // note: copy-and-swap would always cause a reallocation
};
 
int main()
{
    A a1, a2;
    std::cout << "a1 = a2 calls ";
    a1 = a2; // user-defined copy assignment
 
    B b1, b2;
    b2.s1 = "foo";
    b2.s2 = "bar";
    std::cout << "b1 = b2 calls ";
    b1 = b2; // implicitly-defined copy assignment
    std::cout << "b1.s1 = " << b1.s1 << " b1.s2 = " << b1.s2 <<  '\n';
}

Output:

a1 = a2 calls copy assignment of A
b1 = b2 calls copy assignment of A
b1.s1 = foo b1.s2 = bar