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 | |||
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); |
|
Contents |
[edit] 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 the contents of the object a
with the result of a binary operation between the previous value of a
and the value of b
.
[edit] 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 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 is a reference type, the assignment operator applies to 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
- the expression E1 = {} is equivalent to E1 = T(), where
T
is the type ofE1
. - the expression E1 = {E2} is equivalent to E1 = T(E2), where
T
is the type ofE1
, except that narrowing implicit conversions are prohibited.
For class types, this syntax generates a call to the assignment operator with std::initializer_list as the argument, following the rules of list-initialization
[edit] Example
#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
[edit] Builtin compound assignment
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:
A1& operator*=(A1&, A2);
|
||
A1& operator/=(A1&, A2);
|
||
A1& operator+=(A1&, A2);
|
||
A1& operator-=(A1&, A2);
|
||
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:
I1& operator%=(I1&, I2);
|
||
I1& operator<<=(I1&, I2);
|
||
I1& operator>>=(I1&, I2);
|
||
I1& operator&=(I1&, I2);
|
||
I1& operator^=(I1&, I2);
|
||
I1& operator|=(I1&, I2);
|
||
For every optionally cv-qualified object type T
, the following function signatures participate in overload resolution:
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);
|
||
The behavior of every builtin compound-assignment expression E1 op= E2 is exactly the same as the behavior of the expression E1 = E1 op E2, except that the expression E1
is evaluated only once and that it behaves as a single operation with respect to indeterminately-sequenced function calls (e.g. in f(a += b, g()), the += is either not started at all or is completed as seen from inside g()).
[edit] Example
This section is incomplete Reason: no example |
[edit] See also
Common operators | ||||||
---|---|---|---|---|---|---|
assignment | increment decrement |
arithmetic | logical | comparison | member access |
other |
a = b |
++a |
+a |
!a |
a == b |
a[b] |
a(...) |
Special operators | ||||||
static_cast converts one type to another compatible type |