Namespaces
Variants
Views
Actions

Assignment operators

From cppreference.com
< cpp‎ | language
Revision as of 08:33, 25 April 2011 by WikiSysop (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Template:cpp/language/sidebar Assignment operators modify the value of the object.

Operator name Syntax Over​load​able Prototype examples (for Template:cpp)
Inside class definition Outside class definition
basic assignment a = b Yes Template:cpp N/A
move assignment Template:mark c++0x a = rvalue Yes Template:cpp N/A
addition assignment a += b Yes Template:cpp Template:cpp
subtraction assignment a -= b Yes Template:cpp Template:cpp
multiplication assignment a *= b Yes Template:cpp Template:cpp
division assignment a /= b Yes Template:cpp Template:cpp
modulo assignment a %= b Yes Template:cpp Template:cpp
bitwise AND assignment a &= b Yes Template:cpp Template:cpp
bitwise OR assignment a |= b Yes Template:cpp Template:cpp
bitwise XOR assignment a ^= b Yes Template:cpp Template:cpp
bitwise left shift assignment a <<= b Yes Template:cpp Template:cpp
bitwise right shift assignment a >>= b Yes Template:cpp Template:cpp
Notes
  • All operators usually return *this. However, essentially any value and any type can be returned (including Template:cpp,
    i.e. no return value), yet this is unintuitive and defeats the purpose of the operators.
  • T2 can be any type including T

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.

Other assignment operators modify the contents of the object. Usually they are overloaded in classed performing mathematical operations.

See also

Operator precedence

Common operators
assignment increment
decrement
arithmetic logical comparison member
access
other

a = b
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
a->b
a.b
a->*b
a.*b

function call
a(...)
comma
a, b
conditional
a ? b : c
Special operators

static_cast converts one type to another related type
dynamic_cast converts within inheritance hierarchies
const_cast adds or removes cv-qualifiers
reinterpret_cast converts type to unrelated type
C-style cast converts one type to another by a mix of static_cast, const_cast, and reinterpret_cast
new creates objects with dynamic storage duration
delete destructs objects previously created by the new expression and releases obtained memory area
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)