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;
address = &obj;
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++ ) ... 从左到右
 
 

 
 

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 &laquo; typeid(var).name();

cout &laquo; 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

 
原文地址:https://www.cnblogs.com/dirt2/p/5255421.html