Namespaces
Variants
Views
Actions

Assignment operators

From cppreference.com
 
 
C++ language
General topics
Preprocessor
Comments
Keywords
ASCII chart
Escape sequences
History of C++
Flow control
Conditional execution statements
Iteration statements
Jump statements
Functions
function declaration
lambda function declaration
function template
inline specifier
exception specifications (deprecated)
noexcept specifier (C++11)
Exceptions
Namespaces
Types
decltype specifier (C++11)
Specifiers
cv specifiers
storage duration specifiers
constexpr specifier (C++11)
auto specifier (C++11)
alignas specifier (C++11)
Literals
Expressions
alternative representations
Utilities
Types
typedef declaration
type alias declaration (C++11)
attributes (C++11)
Casts
implicit conversions
const_cast conversion
static_cast conversion
dynamic_cast conversion
reinterpret_cast conversion
C-style and functional cast
Memory allocation
Classes
Class-specific function properties
Special member functions
Templates
class template
function template
template specialization
parameter packs (C++11)
Miscellaneous
Inline assembly
 

Assignment operators modify the value of the object.

Operator name Syntax Over​load​able 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);
Notes
  • All operators usually return *this. However, essentially any value and any type can be returned (including void,
    i.e. no return value), yet this is unintuitive and defeats the purpose of the operators.
  • T2 can be any type including T

[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

Operator precedence

Common operators
assignment increment
decrement
arithmetic logical comparison member
access
other

a = b
a = rvalue
a += b
a -= b
a *= b
a /= b
a %= b
a &= b
a |= b
a ^= b
a <<= b
a >>= b

++a
--a
a++
a--

+a
-a
a + b
a - b
a * b
a / b
a % b
~a
a & b
a | b
a ^ b
a << b
a >> b

!a
a && b
a || b

a == b
a != b
a < b
a > b
a <= b
a >= b

a[b]
*a
&a
a->b
a.b
a->*b
a.*b

a(...)
a, b
(type) a
? :

Special operators

static_cast converts one type to another compatible type
dynamic_cast converts virtual base class to derived class
const_cast converts type to compatible type with different cv qualifiers
reinterpret_cast converts type to incompatible type
new allocates memory
delete deallocates memory
sizeof queries the size of a type
sizeof... queries the size of a parameter pack (since C++11)
typeid queries the type information of a type
noexcept checks if an expression can throw an exception (since C++11)
alignof queries alignment requirements of a type (since C++11)