分类: C/C++
2009-02-03 22:55:18
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 |
---|---|---|
Group 1 (no associativity) | ||
:: | Scope resolution operator | Class::age = 2; |
Group 2 | ||
() | Function call | isdigit('1') |
() | Member initalization | c_tor(int x, int y) : _x(x), _y(y*10){}; |
[] | Array access | array[4] = 2; |
-> | Member access from a pointer | ptr->age = 34; |
. | Member access from an object | obj.age = 34; |
++ | Post-increment | for( int i = 0; i < 10; i++ ) cout << i; |
-- | Post-decrement | for( int i = 10; i > 0; i-- ) cout << i; |
const_cast | Special cast | const_cast |
dynamic_cast | Special cast | dynamic_cast |
static_cast | Special cast | static_cast |
reinterpret_cast | Special cast | reinterpret_cast |
typeid | Runtime type information | cout « typeid(var).name(); |
Group 3 (right-to-left associativity) | ||
! | Logical negation | if( !done ) … |
not | Alternate spelling for ! | |
~ | Bitwise complement | flags = ~flags; |
compl | Alternate spelling for ~ | |
++ | Pre-increment | for( i = 0; i < 10; ++i ) cout << i; |
-- | Pre-decrement | for( i = 10; i > 0; --i ) cout << i; |
- | Unary minus | int i = -1; |
+ | Unary plus | int i = +1; |
* | Dereference | int data = *intPtr; |
& | Address of | int *intPtr = &data; |
new | Dynamic memory allocation | long *pVar = new long; |
delete | Deallocating the memory | delete pVar; |
(type) | Cast to a given type | int i = (int) floatNum; |
sizeof | Return size of an object | int size = sizeof(floatNum); |
Group 4 | ||
->* | Member pointer selector | ptr->*var = 24; |
.* | Member object selector | obj.*var = 24; |
Group 5 | ||
* | Multiplication | int i = 2 * 4; |
/ | Division | float f = 10.0 / 3.0; |
% | Modulus | int rem = 4 % 3; |
Group 6 | ||
+ | Addition | int i = 2 + 3; |
- | Subtraction | int i = 5 - 1; |
Group 7 | ||
<< | Bitwise shift left | int flags = 33 << 1; |
>> | Bitwise shift right | int flags = 33 >> 1; |
Group 8 | ||
< | Comparison less-than | if( i < 42 ) … |
<= | Comparison less-than-or-equal-to | if( i <= 42 ) ... |
> | Comparison greater-than | if( i > 42 ) … |
>= | Comparison greater-than-or-equal-to | if( i >= 42 ) ... |
Group 9 | ||
== | Comparison equal-to | if( i == 42 ) ... |
eq | Alternate spelling for == | |
!= | Comparison not-equal-to | if( i != 42 ) … |
not_eq | Alternate spelling for != | |
Group 10 | ||
& | Bitwise AND | flags = flags & 42; |
bitand | Alternate spelling for & | |
Group 11 | ||
^ | Bitwise exclusive OR (XOR) | flags = flags ^ 42; |
xor | Alternate spelling for ^ | |
Group 12 | ||
| | Bitwise inclusive (normal) OR | flags = flags | 42; |
bitor | Alternate spelling for | | |
Group 13 | ||
&& | Logical AND | if( conditionA && conditionB ) … |
and | Alternate spelling for && | |
Group 14 | ||
|| | Logical OR | if( conditionA || conditionB ) ... |
or | Alternate spelling for || | |
Group 15 (right-to-left associativity) | ||
? : | Ternary conditional (if-then-else) | int i = (a > b) ? a : b; |
Group 16 (right-to-left associativity) | ||
= | Assignment operator | int a = b; |
+= | Increment and assign | a += 3; |
-= | Decrement and assign | b -= 4; |
*= | Multiply and assign | a *= 5; |
/= | Divide and assign | a /= 2; |
%= | Modulo and assign | a %= 3; |
&= | Bitwise AND and assign | flags &= new_flags; |
and_eq | Alternate spelling for &= | |
^= | Bitwise exclusive or (XOR) and assign | flags ^= new_flags; |
xor_eq | Alternate spelling for ^= | |
|= | Bitwise normal OR and assign | flags |= new_flags; |
or_eq | Alternate spelling for |= | |
<<= | Bitwise shift left and assign | flags <<= 2; |
>>= | Bitwise shift right and assign | flags >>= 2; |
Group 17 | ||
throw | throw exception | throw EClass(“Message”); |
Group 18 | ||
, | Sequential evaluation operator | for( i = 0, j = 0; i < 10; i++, j++ ) … |