C++运算符优先级

本文详细介绍了C++中的运算符优先级及结合性,并解释了运算符重载的概念及其潜在风险。通过具体例子说明如何进行运算符重载,并强调了正确使用的重要性。

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.

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

Order of Evaluation and of Side Effects

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

Overloading of Operators

Overloading of operators can be very useful and very dangerous. On one hand overloading operators for a class you have created can help with logistics and readability of code. On the other you can overload an operator in such a way it can either obfuscate or just down right break your program. Use carefully.

There are two ways to over load an operator: global function or class member.

Example of overloading with a global function:

     ostream & operator<< (ostream & os, const myClass & rhs);

But to be able to modify any data within a user defined class you have to declare the global function as a friend within the definition of the class.

Example:

     class myClass {
 
// Gives the operator<< function access to 'myData'
// (this declaration should not go in public, private or protected)
friend ostream & operator<< (ostream & lhs, const myClass & rhs);
 
private:
int myData;
}

Overloading with a class member can be done as follows:

     class myClass {
 
public:
// The left hand side of this operator is a pointer to 'this'.
int operator+ (const myClass & rhs);
 
private:
int myData;
}
### C++ 运算符优先级C++运算符优先级定义了表达式中各运算符的计算顺序。当一个复杂表达式包含多个运算符时,优先级较高的运算符会先被执行[^1]。 以下是完整的 C++ 运算符优先级列表(从高到低排列),涵盖了大部分常见的运算符: #### 单目运算符 | 优先级 | 类型 | 运算符 | |--------|--------------------|---------------------------------| | 1 | 后缀 | `()`, `[]`, `->`, `.`, `postfix ++`, `postfix --`[^5] | | 2 | 前缀 | `prefix ++`, `prefix --`, `+`, `-`, `!`, `~`, `(type)`, `*`, `&`, `sizeof`, `new`, `delete`[^3] | #### 多目运算符 | 优先级 | 类型 | 运算符 | |--------|------------------|------------------------------| | 3 | 成员访问 | `.*`, `->*` | | 4 | 求值 | `*`, `/`, `%` | | 5 | 加减法 | `+`, `-` | | 6 | 移位操作 | `<<`, `>>` | | 7 | 关系比较 | `<`, `>`, `<=`, `>=` | | 8 | 相等性判断 | `==`, `!=` | | 9 | 逻辑与 | `&` | | 10 | 逻辑异或 | `^` | | 11 | 逻辑或 | `\|` | | 12 | 条件测试 | `&&` | | 13 | 或者条件测试 | `\|\|` | | 14 | 条件表达式 | `?:` | | 15 | 赋值 | `=`, `+=`, `-=` , `*=` , `/=`, etc.| 需要注意的是,在某些情况下,即使知道了运算符优先级,仍然可能需要通过显式的括号来改变默认的求值顺序[^4]。 对于 C++17 及之后的标准版本新增的一些特殊用途运算符未在此表格中列出,例如三向比较运算符 (`<=>`) 等[^2]。 ```cpp // 示例代码展示如何利用优先级解析表达式 int a = 5, b = 3; bool result = (a + b * 2 >= 10 && !(b < 2)); // 正确理解此语句需掌握优先级规则 ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值