C/C++运算符的优先级

Precedence Operator Description Example Overloadable Associativity
1::Scope resolution operatorClass::age = 2;nonone
2()
()
[]
->
.
++
--
const_cast
dynamic_cast
static_cast
reinterpret_cast
typeid
Function call
Member initalization
Array access
Member access from a pointer
Member access from an object
Post-increment
Post-decrement
Special cast
Special cast
Special cast
Special cast
Runtime type information
isdigit('1')
c_tor(int x, int y) : _x(x), _y(y*10){};
array[4] = 2;
ptr->age = 34;
obj.age = 34;
for( int i = 0; i < 10; i++ ) cout << i;
for( int i = 10; i > 0; i-- ) cout << i;
const_cast<type_to>(type_from);
dynamic_cast<type_to>(type_from);
static_cast<type_to>(type_from);
reinterpret_cast<type_to>(type_from);
cout « typeid(type).name();
yes
yes
yes
yes
no
yes
yes
no
no
no
no
no
left to right
3!
not
~
compl
++
--
-
+
*
&
new
new []
delete
delete []
(type)
sizeof
Logical negation
Alternate spelling for !
Bitwise complement
Alternate spelling for ~
Pre-increment
Pre-decrement
Unary minus
Unary plus
Dereference
Address of
Dynamic memory allocation
Dynamic memory allocation of array
Deallocating the memory
Deallocating the memory of array
Cast to a given type
Return size of an object or type
if( !done ) …

flags = ~flags;

for( i = 0; i < 10; ++i ) cout << i;
for( i = 10; i > 0; --i ) cout << i;
int i = -1;
int i = +1;
int data = *intPtr;
int *intPtr = &data;
long *pVar = new long;
MyClass *ptr = new MyClass(args);
delete pVar;
delete [] array;
int i = (int) floatNum;//int size = sizeof(float);
yes

yes

yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
no
right to left
4->*
.*
Member pointer selector
Member object selector
ptr->*var = 24;
obj.*var = 24;
yes
no
left to right
5*
/
%
Multiplication
Division
Modulus
int i = 2 * 4;
float f = 10.0 / 3.0;
int rem = 4 % 3;
yes
yes
yes
left to right
6+
-
Addition
Subtraction
int i = 2 + 3;
int i = 5 - 1;
yes
yes
left to right
7<<
>>
Bitwise shift left
Bitwise shift right
int flags = 33 << 1;
int flags = 33 >> 1;
yes
yes
left to right
8<
<=
>
>=
Comparison less-than
Comparison less-than-or-equal-to
Comparison greater-than
Comparison greater-than-or-equal-to
if( i < 42 ) …
if( i <= 42 ) ...
if( i > 42 ) …
if( i >= 42 ) ...
yes
yes
yes
yes
left to right
9==
eq
!=
not_eq
Comparison equal-to
Alternate spelling for ==
Comparison not-equal-to
Alternate spelling for !=
if( i == 42 ) ...

if( i != 42 ) …
yes
-
yes
left to right
10&
bitand
Bitwise AND
Alternate spelling for &
flags = flags & 42;
yes
left to right
11^
xor
Bitwise exclusive OR (XOR)
Alternate spelling for ^
flags = flags ^ 42;
yes
left to right
12|
bitor
Bitwise inclusive (normal) OR
Alternate spelling for |
flags = flags | 42;
yes
left to right
13&&
and
Logical AND
Alternate spelling for &&
if( conditionA && conditionB ) …
yes
left to right
14||
or
Logical OR
Alternate spelling for ||
if( conditionA || conditionB ) ...
yes
left to right
15? :Ternary conditional (if-then-else)int i = (a > b) ? a : b;noright to left
16=
+=
-=
*=
/=
%=
&=
and_eq
^=
xor_eq
|=
or_eq
<<=
>>=
Assignment operator
Increment and assign
Decrement and assign
Multiply and assign
Divide and assign
Modulo and assign
Bitwise AND and assign
Alternate spelling for &=
Bitwise exclusive or (XOR) and assign
Alternate spelling for ^=
Bitwise normal OR and assign
Alternate spelling for |=
Bitwise shift left and assign
Bitwise shift right and assign
int a = b;
a += 3;
b -= 4;
a *= 5;
a /= 2;
a %= 3;
flags &= new_flags;

flags ^= new_flags;

flags |= new_flags;

flags <<= 2;
flags >>= 2;
yes
yes
yes
yes
yes
yes
yes

yes

yes

yes
yes
right to left
17throwthrow exceptionthrow EClass(“Message”);no
18,Sequential evaluation operatorfor( i = 0, j = 0; i < 10; i++, j++ ) …yesleft to right

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值