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 =(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); |
|
[edit] Explanation
basic assignment operator replaces the contents of the object a with those of b
move assignment operator replaces the contents of the object a with those of b while minimizing copying overhead (no deep copy is performed). It complements the basic assignment operator. (since C++11)
Other assignment operators modify the contents of the object. Usually they are overloaded in classes performing mathematical operations.
[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 |