Assignment operators
![]() |
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. |
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); |
|
Sommaire |
[modifier] Explication
a
objet avec une copie du contenu de b
(b
n'est modifié). Pour les types de classe, il s'agit d'une fonction membre particulière, décrite dans copier opérateur d'affectation .a
with a copy of the contents of b
(b
is no modified). For class types, this is a special member function, described in copier opérateur d'affectation.You can help to correct and verify the translation. Click here for instructions.
a
objet avec le contenu de b
, en évitant, si possible, la copie (b
peut être modifié). Pour les types de classe, il s'agit d'une fonction membre particulière, décrite dans déplacer opérateur d'affectation. (depuis C++11) 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 déplacer opérateur d'affectation. (depuis C++11) 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.
a
avec le résultat d'une opération binaire entre la valeur précédente de a
et la valeur de b
.a
with the result of a binary operation between the previous value of a
and the value of b
.You can help to correct and verify the translation. Click here for instructions.
[modifier] Builtin affectation directe
T
, les signatures de fonction suivants participent à la résolution de surcharge:T
, the following function signatures participate in overload resolution:You can help to correct and verify the translation. Click here for instructions.
T*& operator=(T*&, T*); |
||
T*volatile & operator=(T*volatile &, T*); |
||
T
type de membre, éventuellement volatile qualifié, la signature de la fonction suivante participe à la résolution de surcharge:T
, optionally volatile-qualified, the following function signature participates in overload resolution:You can help to correct and verify the translation. Click here for instructions.
T& operator=(T&, T ); |
||
You can help to correct and verify the translation. Click here for instructions.
A1& operator=(A1&, A2); |
||
T
type scalaire, les formes suivantes supplémentaires de l'expression d'assignation builtin sont autorisés:T
, the following additional forms of the builtin assignment expression are allowed:You can help to correct and verify the translation. Click here for instructions.
E1 = {} |
(depuis C++11) | |
E1 = {E2} |
(depuis C++11) | |
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.
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.
You can help to correct and verify the translation. Click here for instructions.
- E1 = {} l'expression est équivalente à E1 = T(), où
T
est le type deE1
.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. - E1 = {E2} l'expression est équivalente à E1 = T(E2), où
T
est le type deE1
, sauf que les conversions restrictives implicites sont interdits .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.
You can help to correct and verify the translation. Click here for instructions.
[modifier] Exemple
#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 }
Résultat :
1 0 97 98 1 2
[modifier] Affectation composée intégré
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); |
||
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); |
||
T
option cv-qualifié, les signatures de fonction suivants participent à la résolution de surcharge:T
, the following function signatures participate in overload resolution: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); |
||
You can help to correct and verify the translation. Click here for instructions.
[modifier] Exemple
This section is incomplete Reason: no example |
[modifier] Voir aussi
Opérateurs ordinaires | ||||||
---|---|---|---|---|---|---|
affectation | incrémentation décrémentation |
arithmétique | logique | comparaison | accès aux membre | autre |
a = b |
++a |
+a |
!a |
a == b |
a[b] |
a(...) |
Opérateurs spéciaux | ||||||
static_cast convertit un type dans un autre type compatible dynamic_cast convertit une classe de base virtuelle dans une classe dérivée const_cast convertit un type dans un type compatible avec des cv-qualifiers différents reinterpret_cast convertit un type dans un type incompatibles new allocation de la mémoire delete libère de la mémoire sizeof récupère la taille d'un type sizeof... récupère la taille d'un paquet de paramètres (depuis C++11) typeid récupère les informations de type d'un type noexcept vérifie si une expression peut lancer une exception (depuis C++11) alignof récupère les conditions d'alignement d'un type (depuis C++11) |