C++ 运算符优先级列表

 引用:http://www.cppreference.com/wiki/operator_precedence

 

C++ Operator Precedence

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.

OperatorDescriptionExample
Group 1 (no associativity)
::Scope resolution operatorClass::age = 2;
Group 2
()Function callisdigit('1')
()Member initalizationc_tor(int x, int y) : _x(x), _y(y*10){};
[]Array accessarray[4] = 2;
->Member access from a pointerptr->age = 34;
.Member access from an objectobj.age = 34;
++Post-incrementfor( int i = 0; i < 10; i++ ) cout << i;
--Post-decrementfor( int i = 10; i > 0; i-- ) cout << i;
const_castSpecial castconst_cast<type_to>(type_from);
dynamic_castSpecial castdynamic_cast<type_to>(type_from);
static_castSpecial caststatic_cast<type_to>(type_from);
reinterpret_castSpecial castreinterpret_cast<type_to><type_from>;
typeidRuntime type informationcout « typeid(var).name();
Group 3 (right-to-left associativity)
!Logical negationif( !done ) …
notAlternate spelling for !
~Bitwise complementflags = ~flags;
complAlternate spelling for ~
++Pre-incrementfor( i = 0; i < 10; ++i ) cout << i;
--Pre-decrementfor( i = 10; i > 0; --i ) cout << i;
-Unary minusint i = -1;
+Unary plusint i = +1;
*Dereferenceint data = *intPtr;
&Address ofint *intPtr = &data;
newDynamic memory allocationlong *pVar = new long;
deleteDeallocating the memorydelete pVar;
(type)Cast to a given typeint i = (int) floatNum;
sizeofReturn size of an objectint size = sizeof(floatNum);
Group 4
->*Member pointer selectorptr->*var = 24;
.*Member object selectorobj.*var = 24;
Group 5
*Multiplicationint i = 2 * 4;
/Divisionfloat f = 10.0 / 3.0;
%Modulusint rem = 4 % 3;
Group 6
+Additionint i = 2 + 3;
-Subtractionint i = 5 - 1;
Group 7
<<Bitwise shift leftint flags = 33 << 1;
>>Bitwise shift rightint flags = 33 >> 1;
Group 8
<Comparison less-thanif( i < 42 ) …
<=Comparison less-than-or-equal-toif( i <= 42 ) ...
>Comparison greater-thanif( i > 42 ) …
>=Comparison greater-than-or-equal-toif( i >= 42 ) ...
Group 9
==Comparison equal-toif( i == 42 ) ...
eqAlternate spelling for ==
!=Comparison not-equal-toif( i != 42 ) …
not_eqAlternate spelling for !=
Group 10
&Bitwise ANDflags = flags & 42;
bitandAlternate spelling for &
Group 11
^Bitwise exclusive OR (XOR)flags = flags ^ 42;
xorAlternate spelling for ^
Group 12
|Bitwise inclusive (normal) ORflags = flags | 42;
bitorAlternate spelling for |
Group 13
&&Logical ANDif( conditionA && conditionB ) …
andAlternate spelling for &&
Group 14
||Logical ORif( conditionA || conditionB ) ...
orAlternate 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 operatorint a = b;
+=Increment and assigna += 3;
-=Decrement and assignb -= 4;
*=Multiply and assigna *= 5;
/=Divide and assigna /= 2;
%=Modulo and assigna %= 3;
&=Bitwise AND and assignflags &= new_flags;
and_eqAlternate spelling for &=
^=Bitwise exclusive or (XOR) and assignflags ^= new_flags;
xor_eqAlternate spelling for ^=
|=Bitwise normal OR and assignflags |= new_flags;
or_eqAlternate spelling for |=
<<=Bitwise shift left and assignflags <<= 2;
>>=Bitwise shift right and assignflags >>= 2;
Group 17
throwthrow exceptionthrow EClass(“Message”);
Group 18
,Sequential evaluation operatorfor( i = 0, j = 0; i < 10; i++, j++ ) …

One important aspect of C++ that is related to operator precedence is the orderof evaluation and the order of side effects in expressions. In somecircumstances, 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.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值