Assignment operators
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. |
Zuweisungsoperatoren ändern Sie den Wert des Objekts .
Original:
Assignment operators modify the value of the object.
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.
Operator name | Syntax | Overloadable | Prototype examples (for class T) | |
---|---|---|---|---|
Inside class definition | Outside class definition | |||
basic assignment | a = b
|
Yes | T& T::operator =(const T2& b); | N/A |
move assignment (C++11) | a = rvalue
|
Yes | T& T::operator =(T2&& b); | N/A |
addition assignment | a += b
|
Yes | T& T::operator +=(const T2& b); | T& operator +=(T& a, const T2& b); |
subtraction assignment | a -= b
|
Yes | T& T::operator -=(const T2& b); | T& operator -=(T& a, const T2& b); |
multiplication assignment | a *= b
|
Yes | T& T::operator *=(const T2& b); | T& operator *=(T& a, const T2& b); |
division assignment | a /= b
|
Yes | T& T::operator /=(const T2& b); | T& operator /=(T& a, const T2& b); |
modulo assignment | a %= b
|
Yes | T& T::operator %=(const T2& b); | T& operator %=(T& a, const T2& b); |
bitwise AND assignment | a &= b
|
Yes | T& T::operator &=(const T2& b); | T& operator &=(T& a, const T2& b); |
bitwise OR assignment | a |= b
|
Yes | T& T::operator |=(const T2& b); | T& operator |=(T& a, const T2& b); |
bitwise XOR assignment | a ^= b
|
Yes | T& T::operator ^=(const T2& b); | T& operator ^=(T& a, const T2& b); |
bitwise left shift assignment | a <<= b
|
Yes | T& T::operator <<=(const T2& b); | T& operator <<=(T& a, const T2& b); |
bitwise right shift assignment | a >>= b
|
Yes | T& T::operator >>=(const T2& b); | T& operator >>=(T& a, const T2& b); |
|
Inhaltsverzeichnis |
[Bearbeiten] Erklärung
Zuweisungsoperator Operator ersetzt den Inhalt des Objekts
a
mit einer Kopie des Inhalts b
(b
wird nicht modifiziert). Für Klasse-Typen ist dies eine spezielle Elementfunktion, in Kopieren Zuweisungsoperator beschrieben .Original:
copy assignment operator replaces the contents of the object
a
with a copy of the contents of b
(b
is no modified). For class types, this is a special member function, described in Kopieren Zuweisungsoperator.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.
' Bewegen Zuordnung Operator ersetzt den Inhalt des Objekts
a
mit dem Inhalt b
unter Vermeidung Kopierens wenn möglich (b
kann modifiziert werden). Für Klasse-Typen ist dies eine spezielle Elementfunktion, in bewegen Zuweisungsoperator beschrieben. (seit C++11) Original:
move assignment operator replaces the contents of the object
a
with the contents of b
, avoiding copying if possible (b
may be modified). For class types, this is a special member function, described in bewegen Zuweisungsoperator. (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.
You can help to correct and verify the translation. Click here for instructions.
Für Nicht-Klasse-Typen sind Kopieren und Verschieben Zuordnung zu unterscheiden und werden als direkte Zuordnung bezeichnet .
Original:
For non-class types, copy and move assignment are indistinguishable and are referred to as direct 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.
' Verbundzuweisung Betreiber ersetzen den Inhalt der Inhalt des Objekts
a
mit dem Ergebnis einer binären Operation zwischen dem vorherigen Wert des a
und dem Wert des b
.Original:
compound assignment operators replace the contents the contents of the object
a
with the result of a binary operation between the previous value of a
and the value of b
.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.
[Bearbeiten] Builtin direkte Zuordnung
Für jede Art
T
, beteiligen die folgende Funktion Signaturen in Überlast-Auflösung:Original:
For every type
T
, the following function signatures participate in overload resolution: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*& operator=(T*&, T*); |
||
T*volatile & operator=(T*volatile &, T*); |
||
Für jede Aufzählung oder Zeiger auf Elementtyp
T
, wahlweise flüchtigen qualifizierte, beteiligt sich die folgende Funktion Signatur in Überlast-Auflösung:Original:
For every enumeration or pointer to member type
T
, optionally volatile-qualified, the following function signature participates in overload resolution: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& operator=(T&, T ); |
||
Für jedes Paar A1 und A2, wobei A1 ist ein arithmetischer Typ (optional flüchtigen qualifizierte) und A2 ist ein befördert arithmetischen Typ, beteiligt sich die folgende Funktion Signatur in Überlast-Auflösung:
Original:
For every pair A1 and A2, where A1 is an arithmetic type (optionally volatile-qualified) and A2 is a promoted arithmetic type, the following function signature participates in overload resolution:
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.
A1& operator=(A1&, A2); |
||
Für Ausdrücke E1 jeder skalaren Typ
T
, werden die folgenden zusätzlichen Formen der builtin Zuweisungsausdruck erlaubt:Original:
For expressions E1 of any scalar type
T
, the following additional forms of the builtin assignment expression are allowed: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.
E1 = {} |
(seit C++11) | |
E1 = {E2} |
(seit C++11) | |
Hinweis: Die oben sind alle Nicht-Klasse-Typen außer Referenztypen, Array-Typen, Funktionstypen und der Art void, die nicht direkt zuordenbaren .
Original:
Note: the above includes all non-class types except reference types, array types, function types, and the type void, which are not directly 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.
Die direkte Zuordnung Betreiber rechnet mit einem modifizierbaren lvalue als linken Operanden und gibt eine lvalue Identifizierung des linken Operanden nach der Änderung. Für Nicht-Klasse-Typen, der rechte Operand ersten implizit konvertiert der cv-unqualifizierte Typ des linken Operanden und dann wird sein Wert in das Objekt mit der linken Operanden identifiziert kopiert .
Original:
The direct assignment operator expects a modifiable lvalue as its left operand and returns an lvalue identifying the left operand after modification. For non-class types, the right operand is first implizit konvertiert to the cv-unqualified type of the left operand, and then its value is copied into the object identified by left operand.
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.
Wenn der linke Operand einen Verweistyp ist, gilt die Abtretung Betreiber bezeichnet-to object .
Original:
When the left operand is a reference type, the assignment operator applies to the referred-to object.
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.
Wenn die linke und die rechte Operanden identifizieren überlappenden Objekten, ist das Verhalten undefiniert (es sei denn, die Überlappung genau ist und die Art ist das gleiche)
Original:
If the left and the right operands identify overlapping objects, the behavior is undefined (unless the overlap is exact and the type is the same)
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.
Wenn der rechte Operand ist ein verspannt-init-Liste
Original:
If the right operand is a braced-init-list
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 Ausdruck E1 = {} entspricht E1 = T(), wo
T
die Art derE1
ist .Original:the expression E1 = {} is equivalent to E1 = T(), whereT
is the type ofE1
.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - der Ausdruck E1 = {E2} entspricht E1 = T(E2), wo
T
ist die Art derE1
, außer dass Verengen impliziten Konvertierungen verboten sind .Original:the expression E1 = {E2} is equivalent to E1 = T(E2), whereT
is the type ofE1
, except that narrowing implicit conversions are prohibited.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Für Klasse-Typen generiert diese Syntax einen Aufruf an die Zuweisungsoperator mit std::initializer_list als Argument, nach den Regeln der list-Initialisierung
Original:
For class types, this syntax generates a call to the assignment operator with std::initializer_list as the argument, following the rules of list-Initialisierung
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.
[Bearbeiten] Beispiel
#include <iostream> int main() { int n = 0; // not an assignment n = 1; // direct asignment std::cout << n << ' '; n = {}; // zero-initialization, then assignment std::cout << n << ' '; n = 'a'; // integral promotion, then assignment std::cout << n << ' '; n = {'b'}; // explicit cast, then assignment std::cout << n << ' '; n = 1.0; // floating-point conversion, then assignment std::cout << n << ' '; // n = {1.0}; // compiler error (narrowing conversion) int& r = n; // not an assignment int* p; r = 2; // assignment through reference std::cout << n << ' '; p = &n; // direct assignment p = nullptr; // null-pointer conversion, then assignment }
Output:
1 0 97 98 1 2
[Bearbeiten] Builtin Verbindung Abtretung an
Für jedes Paar A1 und A2, wobei A1 ist ein arithmetischer Typ (optional flüchtigen qualifizierte) und A2 ist ein befördert arithmetischen Typ, beteiligen die folgende Funktion Signaturen in Überlast-Auflösung:
Original:
For every pair A1 and A2, where A1 is an arithmetic type (optionally volatile-qualified) and A2 is a promoted arithmetic type, the following function signatures participate in overload resolution:
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.
A1& operator*=(A1&, A2); |
||
A1& operator/=(A1&, A2); |
||
A1& operator+=(A1&, A2); |
||
A1& operator-=(A1&, A2); |
||
Für jedes Paar I1 und I2, wobei I1 ist ein integraler Typ (optional flüchtigen qualifizierte) und I2 ist ein befördert integraler Typ, die folgende Funktion Signaturen in Überladungsauflösung teilnehmen:
Original:
For every pair I1 and I2, where I1 is an integral type (optionally volatile-qualified) and I2 is a promoted integral type, the following function signatures participate in overload resolution:
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.
I1& operator%=(I1&, I2); |
||
I1& operator<<=(I1&, I2); |
||
I1& operator>>=(I1&, I2); |
||
I1& operator&=(I1&, I2); |
||
I1& operator^=(I1&, I2); |
||
I1& operator|=(I1&, I2); |
||
Für jedes wahlweise cv qualifizierte Objekttyp
T
, die folgende Funktion Signaturen in Überladungsauflösung teilnehmen:Original:
For every optionally cv-qualified object type
T
, the following function signatures participate in overload resolution: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*& operator+=(T*&, std::ptrdiff_t); |
||
T*& operator-=(T*&, std::ptrdiff_t); |
||
T*volatile & operator+=(T*volatile &, std::ptrdiff_t); |
||
T*volatile & operator-=(T*volatile &, std::ptrdiff_t); |
||
{{{1}}}
Original:
{{{2}}}
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.
[Bearbeiten] Beispiel
This section is incomplete Reason: no example |
[Bearbeiten] Siehe auch
Common operators | ||||||
---|---|---|---|---|---|---|
Zuweisungen | incrementNJdecrement | Arithmetik | logisch | Vergleich | memberNJaccess | andererseits |
a = b |
++a |
+a |
!a |
a == b |
a[b] |
a(...) |
Special operators | ||||||
static_cast wandelt einem Typ in einen anderen kompatiblen Typ
Original: static_cast converts one type to another compatible type The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. dynamic_cast wandelt virtuellen Basisklasse abgeleitet class
Original: dynamic_cast converts virtual base class to derived class The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. const_cast wandelt Typ kompatiblen Typ mit unterschiedlichen cv qualifiers
Original: The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. reinterpret_cast wandelt Typ inkompatibel type
Original: reinterpret_cast converts type to incompatible type The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. new ordnet memory
Original: new allocates memory The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. delete freigibt memory
Original: delete deallocates memory The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. sizeof fragt die Größe eines type
Original: sizeof queries the size of a type The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. sizeof... fragt die Größe eines Parameter Pack (seit C++11)
Original: The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. typeid fragt die Typinformationen eines type
Original: typeid queries the type information of a type The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. noexcept prüft, ob ein Ausdruck eine Ausnahme (seit C++11)
werfen kann Original: The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. alignof Abfragen Ausrichtungsanforderungen eines Typs (seit C++11)
Original: alignof queries alignment requirements of a 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. |