C++运算符优先级
在一个表达式中可能包含多个有不同运算符连接起来的、具有不同数据类型的数据对象;由于表达式有多种运算,不同的运算顺序可能得出不同结果甚至出现错误运算错误,因为当表达式中含多种运算时,必须按一定顺序进行结合,才能保证运算的合理性和结果的正确性、唯一性。
优先级从上到下依次递减,最上面具有最高的优先级,逗号操作符具有最低的优先级。表达式的结合次序取决于表达式中各种运算符的优先级。优先级高的运算符先结合,优先级低的运算符后结合,同一行中的运算符的优先级相同。
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 « typeid(var).name();
cout « 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
|
本文来自博客园,作者:Arthurian,转载请注明原文链接:https://www.cnblogs.com/Arthurian/p/9020428.html
欢迎邮件交流:zhuanxinxin@aliyun.com
分类:
编程语言 / C/C++
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
2017-05-10 C++获取系统当前时间
2017-05-10 C#中switch的使用
2017-05-10 C++判断一个数字是否为质数
2017-05-10 递归