Assignment operators
Assignment operators modify the value of the object.
Operator name | Syntax | Overloadable | Prototype examples (for class T) | |
---|---|---|---|---|
Inside class definition | Outside class definition | |||
simple assignment | a = b
|
Yes | T& T::operator =(const 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); |
|
Explanation
copy assignment operator replaces the contents of the object a
with a copy of the contents of b
(b
is not modified). For class types, this is a special member function, described in copy assignment operator.
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 move assignment operator. (since C++11)
For non-class types, copy and move assignment are indistinguishable and are referred to as direct assignment.
compound assignment operators replace the contents of the object a
with the result of a binary operation between the previous value of a
and the value of b
.
Builtin direct assignment
For every type T
, the following function signatures participate in overload resolution:
T*& operator=(T*&, T*); |
||
T*volatile & operator=(T*volatile &, T*); |
||
For every enumeration or pointer to member type T
, optionally volatile-qualified, the following function signature participates in overload resolution:
T& operator=(T&, T ); |
||
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:
A1& operator=(A1&, A2); |
||
For expressions E1 of any scalar type T
, the following additional forms of the builtin assignment expression are allowed:
E1 = {} |
(since C++11) | |
E1 = {E2} |
(since C++11) | |
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 direct assignment operator expects a modifiable lvalue as its left operand and an rvalue expression or a braced-init-list (since C++11) as its right operand, and returns an lvalue identifying the left operand after modification.
For non-class types, the right operand is first implicitly converted to the cv-unqualified type of the left operand, and then its value is copied into the object identified by left operand.
When the left operand has reference type, the assignment operator modifies the referred-to object.
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)
If the right operand is a braced-init-list
|
(since C++11) |