Copy assignment operator
Aus cppreference.com
[] |
This page has been machine-translated from the English version of the wiki using Google Translate.
The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
<metanoindex/>
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.
You can help to correct and verify the translation. Click here for instructions.
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) | |||||||
Erklrung
# Typische Deklaration einer Zuweisungsoperator, wenn copy-and-swap idiom verwendet werden knnen
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.
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.
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.
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.
You can help to correct and verify the translation. Click here for instructions.
Der Zuweisungsoperator wird aufgerufen, wenn durch berlast Auflsung, zB ausgewhlte wenn ein Objekt auf der linken Seite einer Zuweisung Ausdruck .
Original:
The copy assignment operator is called whenever selected by berlast Auflsung, 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.
You can help to correct and verify the translation. Click here for instructions.
Implizit deklarierte Zuweisungsoperator
Wenn keine benutzerdefinierten Kopie Zuweisungsoperatoren fr eine Klasse-Typ (
struct, class oder union) vorgesehen sind, wird der Compiler immer erklren, ein als Inline ffentlichen Member der Klasse. Diese implizit deklariert Zuweisungsoperator hat die Form T& T::operator=(const T&), wenn alle der folgenden Bedingungen erfllt 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.
You can help to correct and verify the translation. Click here for instructions.
- Jede direkte Basis
BderThat einen Zuweisungsoperator, deren ParameterBoderconst B&oderconst volatile B&Original:each direct baseBofThas a copy assignment operator whose parameters areBorconst B&orconst 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
MderTder Klasse-Typ oder ein Array von Klasse-Typ hat eine Kopie Zuweisungsoperator deren ParameterModerconst M&oderconst volatile M&Original:each non-static data memberMofTof class type or array of class type has a copy assignment operator whose parameters areMorconst M&orconst 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&) erklrt. (Beachten Sie, dass aufgrund dieser Regeln, die implizit deklariert Zuweisungsoperator kann nicht auf einen flchtigen 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.
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 Schlsselwort 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.
You can help to correct and verify the translation. Click here for instructions.
Da der Zuweisungsoperator ist immer fr 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 knnte das gleiche wie das Argument Typ des impliziten Zuweisungsoperator der abgeleiteten Klasse zu sein, wird die using-Deklaration auch von der impliziten versteckt Erklrung .
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.
You can help to correct and verify the translation. Click here for instructions.
Gelschte implizit deklariert Zuweisungsoperator
Die implizit deklariert oder ausgefallen Zuweisungsoperator fr die Klasse
T ist definiert als gelscht 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.
You can help to correct and verify the translation. Click here for instructions.
Teinen nicht-statisches Datenelement, dasconstistOriginal:Thas a non-static data member that isconstThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.That eine nicht-statische Daten Mitglied einer Referenz-Typ .Original:Thas 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.That eine nicht-statische Daten Mitglied, die nicht kopiergeschtzt zugeordnet werden knnen (gelscht hat, unzugnglich oder mehrdeutig Zuweisungsoperator)Original:Thas 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.That direkte oder virtuelle Basisklasse, die nicht kopiergeschtzt zugeordnet werden knnen (gelscht hat, unzugnglich oder mehrdeutig move Zuweisungsoperator)Original:Thas 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.Tverfgt ber eine benutzerfreundliche erklrt move KonstruktorOriginal:Thas a user-declared move constructorThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.Tverfgt ber eine benutzerfreundliche erklrt move ZuweisungsoperatorOriginal:Thas a user-declared move assignment operatorThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Trivial Zuweisungsoperator
Die implizit deklariert Zuweisungsoperator fr die Klasse
T ist trivial, wenn alle der folgenden Bedingungen erfllt 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.
You can help to correct and verify the translation. Click here for instructions.
That keine virtuelle Member-FunktionenOriginal:Thas no virtual member functionsThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.That keine virtuellen BasisklassenOriginal:Thas no virtual base classesThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.- Der Zuweisungsoperator fr jede direkte Basis von
Tgewhlt ist trivialOriginal:The copy assignment operator selected for every direct base ofTis trivialThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - Der Zuweisungsoperator fr jeden nicht-statische Klasse-Typ (oder ein Array von Klasse-Typ) memeber der
Tgewhlt ist trivialOriginal:The copy assignment operator selected for every non-static class type (or array of class type) memeber ofTis trivialThe 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.
You can help to correct and verify the translation. Click here for instructions.
Zuweisungsoperator implizit definiert
Wenn die implizit deklariert Zuweisungsoperator nicht gelscht oder trivial, ist es definiert (das heit, eine Funktion Krper erzeugt und kompiliert) durch den Compiler. Fr
union Typen, kopiert das implizit definierten Zuweisungsoperator die Objekt-Reprsentation (wie std::memmove). Fr Nicht-union-Klasse-Typen (class und struct), fhrt der Bediener Mitglied-weise Kopie Zuordnung des Objekts Basen und nicht-statische Mitglieder, in deren Initialisierung Reihenfolge mit, Verwendung des eingebauten Zuordnung fr die Skalare und Zuweisungsoperator fr 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.
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 verfgt ber eine benutzerfreundliche erklrt destructor oder benutzerdefinierte erklrt 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.
You can help to correct and verify the translation. Click here for instructions.
Notes
Wenn beide kopieren und verschieben Zuweisungsoperatoren vorgesehen sind, whlt berladungsauflsung den Umzug Zuordnung, wenn das Argument ein rvalue (entweder prvalue wie eine namenlose temporre oder xWert, wie das Ergebnis der std::move ), und whlt den Zuweisungsoperator, wenn das Argument lvalue (benannte Objekt oder eine Funktion / Betreiber wieder lvalue Referenz). Wenn nur die Kopie Zuordnung haben, whlen Sie alle Argumente Kategorien es (wie lange es dauert ihr Argument als Wert oder als Verweis auf const, da rvalues const Referenzen binden knnen), was Zuweisungsoperator das Fallback fr unterwegs Zuordnung beim Bewegen nicht verfgbar 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.
You can help to correct and verify the translation. Click here for instructions.
Kopieren und tauschen
Kopieren Zuweisungsoperator kann im Hinblick auf die Copy-Konstruktor, Destruktor und der Swap () Funktion ausgedrckt werden, wenn man zur Verfgung:
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.
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
Fr Nicht-Werfen swap (), bietet diese Form starke Ausnahme Garantie. Fr 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.
You can help to correct and verify the translation. Click here for instructions.
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