Arithmetic Operators: Operator Name Syntax Overloadable Included in Prototype Example (T1 and T2 Are Types)
Arithmetic Operators: Operator Name Syntax Overloadable Included in Prototype Example (T1 and T2 Are Types)
object names, or lvalues, as appropriate. "Overloadable" means that the operator is overloadable in C++. "Included in C" means that the operator exists and has a semantic meaning in C (operators are not overloadable in C). [edit]Arithmetic Operator name Unary plus Addition (sum) Prefix increment Syntax
+a a + b ++a
operators
Prototype example (T1 and T2 are types)
T1 operator +(const T1& a); T1 operator+(const T1& a, const T1& b); T1& operator++ (T1& a); T1 operator++ (T1& a, int);
Postfix increment
a++
(The int argument is required by Yes Yes C++ to differentiate the operator from a prefix-increment operator.)
Assignment by addition Unary minus (negation) Subtraction (difference) Prefix decrement Postfix decrement Assignment by subtraction Multiplication (product) Assignment by multiplication Division (quotient) Assignment of division Modulo (remainder) Assignment of modulo
a += b -a a - b --a a-a -= b a * b a *= b a / b a /= b a % b a %= b
Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes
Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes
T1& operator+=(T1& a, const T1& b); T1 operator-(const T1& a); T1 operator-(const T1& a, const T1& b); T1& operator--(T1& a); T1 operator--(T1& a, int); T1& operator-=(T1& a, const T1& b); T1 operator*(const T1 &a, const T1& b); T1& operator*=(T1& a, const T1& b); T1 operator/(const T1& a, const T1& b); T1& operator/=(T1& a, const T1& b); T1 operator%(const T1& a, const T1& b); T1& operator%=(T1& a, const T1& b);
[edit]Comparison
operators/Relational operators
Operator name
Syntax
Overloadable
Included in C
Prototype example
bool operator<(const T1& a, const T2& b);
Less than
a < b
Yes
Yes
Less than or equal to Greater than Greater than or equal to Not equal to Equal to
bool operator<=(const T1& a, const T2& b); bool operator>(const T1& a, const T2& b); bool operator>=(const T1& a, const T2& b); bool operator!=(const T1& a, const T2& b); bool operator==(const T1& a, const T2& b);
operators
Included in C Yes Yes Yes Prototype example
bool operator!(const T1& a); bool operator&&(const T1& a, const T2& b); bool operator||(const T1& a, const T2& b);
Syntax
!a a && b a || b
operators
Included in C Yes Yes Yes Yes Yes Yes Yes Prototype example
T1 operator<<(const T1& a, const T2& b); T1& operator<<=(T1& a, const T2& b); T1 operator>>(const T1& a, const T2& b); T1& operator>>=(T1& a, const T2& b); T1 operator~(const T1& a); T1 operator&(const T1& a, const T2& b); T1& operator&=(T1& a, const T2& b);
Operator name Bitwise left shift Assignment by bitwise left shift Bitwise right shift Assignment by bitwise right shift Bitwise one's complement (NOT) Bitwise AND Assignment by bitwise AND
Syntax
a << b a <<= b a >> b a >>= b ~a a & b a &= b
a | b a |= b a ^ b a ^= b
T1 operator|(const T1& a, const T2& b); T1& operator|=(T1& a, const T2& b); T1 operator^(const T1& a, const T2& b); T1& operator^=(T1& a, const T2& b);
operators
Included in C Yes Yes Yes Yes Yes Yes Yes No No Prototype example
T1& T1::operator= (const T2& b); void operator() (T1& a); const T2& operator[] (const T1& a, const T2& b); T2& operator* (T1& a); T2* operator& (T1& a); T2* T1::operator-> (); // must be a member function
Operator name Basic assignment Function call Array subscript Indirection (dereference) Address-of (reference) Member by pointer Member Bind pointer to member by pointer Bind pointer to member by reference cast
Syntax
a = b a() a[b] *a &a a->b a.b a->*b a.*b
(type) a
Yes
Yes
comma ternary conditional Scope resolution Pointer to member size-of Type identification Allocate storage Allocate storage (array)
a , b a ? b : c a::b a::*b sizeof a sizeof(type) typeid(a) typeid(type) new type new type[n]
delete a delete[] a
Yes Yes
No No
precedence
The following is a table that lists the precedence and associativity of all the operators in the C++ and C programming languages (when the operators also exist inJava, Perl, PHP and many other recent languages the precedence is the same as that given). Operators are listed top to bottom, in descending precedence. Descending precedence refers to the priority of evaluation. Considering an expression, an operator which is listed on some row will be evaluated prior to any operator that is listed on a row further below it. Operators that are in the same cell (there may be several rows of operators listed in a cell) are evaluated with the same precedence, in the given direction. An operator's precedence is unaffected by overloading. The syntax of expressions in C and C++ is specified by a context-free grammar.[citation needed] The table given here has been inferred from the grammar.[citation needed] A precedence table, while mostly adequate, cannot resolve a few details. In particular, note that the ternary operator allows any arbitrary expression as its middle operand, despite being listed as having higher precedence than the assignment and comma operators. Thus a ? b , c : d is interpreted as a ? (b, c) : d, and not as the meaningless (a ? b), (c : d). Also, note that the immediate, unparenthesized result of a C cast expression cannot be the operand of sizeof. Therefore, sizeof (int) * x is interpreted as (sizeof(int)) * x and not sizeof ((int)
*x).
Precedence Operator
Description
Associativity
::
Left-to-right
++ --
()
Function call
[]
Array subscripting
->
typeid()
const_cast
dynamic_cast
static_cast
++ --
Right-to-left
+-
!~
(type)
Type cast
3
*
Indirection (dereference)
&
Address-of
sizeof
Size-of
new new[]
delete delete[]
.* ->*
Left-to-right
*/%
+-
<< >>
< <=
8
> >=
== !=
10
&
Bitwise AND
11
12
13
&&
Logical AND
14
||
Logical OR
15
c ? t : f
Right-to-Left
+= -=
16
*= /= %=
<<= >>=
&= ^= |=
17
throw
18
Comma
Left-to-right