The operators at the top of this list are evaluated first. Operators within a group have the same precedence. All operators have left-to-right associativity unless otherwise noted.
Operator | Description | Example | Overloadable |
---|---|---|---|
Group 1 (no associativity) |
|||
:: | Scope resolution operator | Class::age = 2; | NO |
Group 2 | |||
() | Function call | isdigit('1') | YES |
() | Member initalization | c_tor(int x, int y) : _x(x), _y(y*10){}; | YES |
[] | Array access | array[4] = 2; | YES |
-> | Member access from a pointer | ptr->age = 34; | YES |
. | Member access from an object | obj.age = 34; | NO |
++ | Post-increment | for( int i = 0; i < 10; i++ ) cout << i; | YES |
-- | Post-decrement | for( int i = 10; i > 0; i-- ) cout << i; | YES |
const_cast | Special cast | const_cast<type_to>(type_from); | NO |
dynamic_cast | Special cast | dynamic_cast<type_to>(type_from); | NO |
static_cast | Special cast | static_cast<type_to>(type_from); | NO |
reinterpret_cast | Special cast | reinterpret_cast<type_to>(type_from); | NO |
typeid | Runtime type information | cout « typeid(var).name(); cout « typeid(type).name(); | NO |
Group 3 (right-to-left associativity) |
|||
! | Logical negation | if( !done ) … | YES |
not | Alternate spelling for ! | ||
~ | Bitwise complement | flags = ~flags; | YES |
compl | Alternate spelling for ~ | ||
++ | Pre-increment | for( i = 0; i < 10; ++i ) cout << i; | YES |
-- | Pre-decrement | for( i = 10; i > 0; --i ) cout << i; | YES |
- | Unary minus | int i = -1; | YES |
+ | Unary plus | int i = +1; | YES |
* | Dereference | int data = *intPtr; | YES |
& | Address of | int *intPtr = &data; | YES |
new | Dynamic memory allocation | long *pVar = new long; MyClass *ptr = new MyClass(args); | YES |
new [] | Dynamic memory allocation of array | long *array = new long[n]; | YES |
delete | Deallocating the memory | delete pVar; | YES |
delete [] | Deallocating the memory of array | delete [] array; | YES |
(type) | Cast to a given type | int i = (int) floatNum; | YES |
sizeof | Return size of an object or type | int size = sizeof floatNum; int size = sizeof(float); | NO |
Group 4 | |||
->* | Member pointer selector | ptr->*var = 24; | YES |
.* | Member object selector | obj.*var = 24; | NO |
Group 5 | |||
* | Multiplication | int i = 2 * 4; | YES |
/ | Division | float f = 10.0 / 3.0; | YES |
% | Modulus | int rem = 4 % 3; | YES |
Group 6 | |||
+ | Addition | int i = 2 + 3; | YES |
- | Subtraction | int i = 5 - 1; | YES |
Group 7 | |||
<< | Bitwise shift left | int flags = 33 << 1; | YES |
>> | Bitwise shift right | int flags = 33 >> 1; | YES |
Group 8 | |||
< | Comparison less-than | if( i < 42 ) … | YES |
<= | Comparison less-than-or-equal-to | if( i <= 42 ) ... | YES |
> | Comparison greater-than | if( i > 42 ) … | YES |
>= | Comparison greater-than-or-equal-to | if( i >= 42 ) ... | YES |
Group 9 | |||
== | Comparison equal-to | if( i == 42 ) ... | YES |
eq | Alternate spelling for == | ||
!= | Comparison not-equal-to | if( i != 42 ) … | YES |
not_eq | Alternate spelling for != | ||
Group 10 | |||
& | Bitwise AND | flags = flags & 42; | YES |
bitand | Alternate spelling for & | ||
Group 11 | |||
^ | Bitwise exclusive OR (XOR) | flags = flags ^ 42; | YES |
xor | Alternate spelling for ^ | ||
Group 12 | |||
| | Bitwise inclusive (normal) OR | flags = flags | 42; | YES |
bitor | Alternate spelling for | | ||
Group 13 | |||
&& | Logical AND | if( conditionA && conditionB ) … | YES |
and | Alternate spelling for && | ||
Group 14 | |||
|| | Logical OR | if( conditionA || conditionB ) ... | YES |
or | Alternate spelling for || | ||
Group 15 (right-to-left associativity) |
|||
? : | Ternary conditional (if-then-else) | int i = (a > b) ? a : b; | NO |
Group 16 (right-to-left associativity) |
|||
= | Assignment operator | int a = b; | YES |
+= | Increment and assign | a += 3; | YES |
-= | Decrement and assign | b -= 4; | YES |
*= | Multiply and assign | a *= 5; | YES |
/= | Divide and assign | a /= 2; | YES |
%= | Modulo and assign | a %= 3; | YES |
&= | Bitwise AND and assign | flags &= new_flags; | YES |
and_eq | Alternate spelling for &= | ||
^= | Bitwise exclusive or (XOR) and assign | flags ^= new_flags; | YES |
xor_eq | Alternate spelling for ^= | ||
|= | Bitwise normal OR and assign | flags |= new_flags; | YES |
or_eq | Alternate spelling for |= | ||
<<= | Bitwise shift left and assign | flags <<= 2; | YES |
>>= | Bitwise shift right and assign | flags >>= 2; | YES |
Group 17 | |||
throw | throw exception | throw EClass(“Message”); | NO |
Group 18 | |||
, | Sequential evaluation operator | for( i = 0, j = 0; i < 10; i++, j++ ) … | YES |
One important aspect of C++ that is related to operator precedence is the order of evaluation and the order of side effects in expressions. In some circumstances, the order in which things happen is not defined. For example, consider the following code:
float x = 1; x = x / ++x;
The value of x is not guaranteed to be consistent across different compilers, because it is not clear whether the computer should evaluate the left or the right side of the division first. Depending on which side is evaluated first, x could take a different value.
Furthermore, while ++x evaluates to x+1, the side effect of actually storing that new value in x could happen at different times, resulting in different values for x.
The bottom line is that expressions like the one above are horribly ambiguous and should be avoided at all costs. When in doubt, break a single ambiguous expression into multiple expressions to ensure that the order of evaluation is correct.
Overloading of operators can be very useful and very dangerous. On one hand overloading operators for a class you have created can help with logistics and readability of code. On the other you can overload an operator in such a way it can either obfuscate or just downright break your program. Use carefully.
There are two ways to over load an operator: global function or class member.
Example of overloading with a global function:
ostream & operator<< (ostream & os, const myClass & rhs);
But to be able to reach any private data within a user defined class you have to declare the global function as a friend within the definition of the class.
Example:
class myClass { // Gives the operator<< function access to 'myData' // (this declaration should not go in public, private or protected) friend ostream & operator<< (ostream & lhs, const myClass & rhs); private: int myData; }
Overloading with a class member can be done as follows:
class myClass { public: // The left hand side of this operator is a pointer to 'this'. int operator+ (const myClass & rhs); private: int myData; }