# C++运算符优先级

1 ()
[]
->
.
::
++
--

(a + b) / 4;
array[4] = 2;
ptr->age = 34;
obj.age = 34;
Class::age = 2;
for( i = 0; i < 10; i++ ) ...
for( i = 10; i > 0; i-- ) ...

2 !
~
++
--
-
+
*
&
(type)
sizeof

if( !done ) ...
flags = ~flags;
for( i = 0; i < 10; ++i ) ...
for( i = 10; i > 0; --i ) ...
int i = -1;
int i = +1;
data = *ptr;
int i = (int) floatNum;
int size = sizeof(floatNum);

3 ->*
.*

ptr->*var = 24;
obj.*var = 24;

4 *
/
%

int i = 2 * 4;
float f = 10 / 3;
int rem = 4 % 3;

5 +
-

int i = 2 + 3;
int i = 5 - 1;

6 <<
>>

int flags = 33 << 1;
int flags = 33 >> 1;

7 <
<=
>
>=

if( i < 42 ) ...
if( i <= 42 ) ...
if( i > 42 ) ...
if( i >= 42 ) ...

8 ==
!=

if( i == 42 ) ...
if( i != 42 ) ...

9 & 按位与操作符 flags = flags & 42; 从左到右
10 ^ 按位异或操作符 flags = flags ^ 42; 从左到右
11 | 按位或操作符 flags = flags | 42; 从左到右
12 && 逻辑与操作符 if( conditionA && conditionB ) ... 从左到右
13 || 逻辑或操作符 if( conditionA || conditionB ) ... 从左到右
14 ? : 三元条件操作符 int i = (a > b) ? a : b; 从右到左
15 =
+=
-=
*=
/=
%=
&=
^=
|=
<<=
>>=

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;

16 , 逗号操作符 for( i = 0, j = 0; i < 10; i++, j++ ) ... 从左到右

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120