C++ Programming/Operators Table

Table of operators

Operators in the same group have the same precedence and the order of evaluation is decided by the associativity (left-to-right or right-to-left). Operators in a preceding group have higher precedence than those in a subsequent group.

Note: Binding of operators actually cannot be completely described by "precedence" rules, and as such this table is an approximation. Correct understanding of the rules requires an understanding of the grammar of expressions.

OperatorsDescriptionExample UsageAssociativity
Scope Resolution Operator
::unary scope resolution operator
for globals
::NUM_ELEMENTS
::binary scope resolution operator
for class and namespace members
std::cout

Function Call, Member Access, Post-Increment/Decrement Operators, RTTI and C++ Casts Left to right
()function call operatorswap (x, y)
[]array index operatorarr [i]
.member access operator
for an object of class/union type
or a reference to it
obj.member
->member access operator
for a pointer to an object of
class/union type
ptr->member
++ --post-increment/decrement operatorsnum++
typeid()run time type identification operator
for an object or type
typeid (std::cout)
typeid (std::iostream)
static_cast<>()
dynamic_cast<>()
const_cast<>()
reinterpret_cast<>()
C++ style cast operators
for compile-time type conversion
See Type Casting for more info
static_cast<float> (i)
dynamic_cast<std::istream> (stream)
const_cast<char*> ("Hello, World!")
reinterpret_cast<const long*> ("C++")
type()functional cast operator
(static_cast is preferred
for conversion to a primitive type)
float (i)
also used as a constructor call
for creating a temporary object, esp.
of a class type
std::string ("Hello, world!", 0, 5)

Unary Operators Right to left
!, notlogical not operator!eof_reached
~, complbitwise not operator~mask
+ -unary plus/minus operators-num
++ --pre-increment/decrement operators++num
&, bitandaddress-of operator&data
*indirection operator*ptr
new
new[]
new()
new()[]
new operators
for single objects or arrays
new std::string (5, '*')
new int [100]
new (raw_mem) int
new (arg1, arg2) int [100]
delete
delete[]
delete operator
for pointers to single objects or arrays
delete ptr
delete[] arr
sizeof
sizeof()
sizeof operator
for expressions or types
sizeof 123
sizeof (int)
(type)C-style cast operator (deprecated)(float)i

Member Pointer Operators Right to left
.*member pointer access operator
for an object of class/union type
or a reference to it
obj.*memptr
->*member pointer access operator
for a pointer to an object of
class/union type
ptr->*memptr

Multiplicative Operators Left to right
* / %multiplication, division and
modulus operators
celsius_diff * 9 / 5

Additive Operators Left to right
+ -addition and subtraction operatorsend - start + 1

Bitwise Shift Operators Left to right
<<
>>
left and right shift operatorsbits << shift_len
bits >> shift_len

Relational Inequality Operators Left to right
< > <= >=less-than, greater-than, less-than or
equal-to, greater-than or equal-to
i < num_elements

Relational Equality Operators Left to right
== !=, not_eqequal-to, not-equal-tochoice != 'n'

Bitwise And Operator Left to right
&, bitandbits & clear_mask_complement

Bitwise Xor Operator Left to right
^, xorbits ^ invert_mask

Bitwise Or Operator Left to right
|, bitorbits | set_mask

Logical And Operator Left to right
&&, andarr != 0 && arr->len != 0

Logical Or Operator Left to right
||, orarr == 0 || arr->len == 0

Conditional Operator Right to left
?:size >= 0 ? size : 0

Assignment Operators Right to left
=assignment operatori = 0
+= -= *= /= %=
&=, and_eq
|=, or_eq
^=, xor_eq <<= >>=
shorthand assignment operators
(foo op= bar represents
foo = foo op bar)
num /= 10

Exceptions
throwthrow "Array index out of bounds"

Comma Operator Left to right
,i = 0, j = i + 1, k = 0
Category:Book:C++ Programming#Operators%20Table%20
Category:Book:C++ Programming