运算符优先级 ================ .. raw:: html .. role:: f .. table:: 运算符的优先级 :align: center +------------------+------------------+------------------+ | 优先级 | 运算符 | 描述 | +==================+==================+==================+ | 0 | :f:`::` | 作用域运算符 | +------------------+------------------+------------------+ | | :f:`[]` | 下标运算 | + +------------------+------------------+ | | :f:`()` | 圆括号 | + +------------------+------------------+ | | :f:`->` | 指向运算符 | + 1 +------------------+------------------+ | | :f:`.` | 成员运算符 | + +------------------+------------------+ | | :f:`++` | 自增(后缀) | + +------------------+------------------+ | | :f:`-\-` | 自减(后缀) | +------------------+------------------+------------------+ | | :f:`!` | 逻辑非 | + +------------------+------------------+ | | :f:`~` | 按位反 | + +------------------+------------------+ | | :f:`++` | 自增(前缀) | + +------------------+------------------+ | | :f:`-\-` | 自减(前缀) | + +------------------+------------------+ | | :f:`-` | 负号 | + 2 +------------------+------------------+ | | :f:`+` | 正号 | + +------------------+------------------+ | | :f:`(类型)` | 类型转换 | + +------------------+------------------+ | | :f:`*` | 指针运算符 | + +------------------+------------------+ | | :f:`&` | 取地址 | + +------------------+------------------+ | | :f:`sizeof` | 长度运算 | +------------------+------------------+------------------+ | | :f:`*` | 乘法 | + +------------------+------------------+ | 3 | :f:`/` | 除法 | + +------------------+------------------+ | | :f:`%` | 求余 | +------------------+------------------+------------------+ | | :f:`+` | 加法 | + 4 +------------------+------------------+ | | :f:`-` | 减法 | +------------------+------------------+------------------+ | | :f:`<<` | 左移位 | + 5 +------------------+------------------+ | | :f:`>>` | 右移位 | +------------------+------------------+------------------+ | 6 | :f:`< <= > >=` | 关系运算符 | +------------------+------------------+------------------+ | | :f:`==` | 等于 | + 7 +------------------+------------------+ | | :f:`!=` | 不等于 | +------------------+------------------+------------------+ | 8 | :f:`&` | 按位与 | +------------------+------------------+------------------+ | 9 | :f:`^` | 按位异或 | +------------------+------------------+------------------+ | 10 | :f:`|` | 按位或 | +------------------+------------------+------------------+ | 11 | :f:`&&` | 逻辑与 | +------------------+------------------+------------------+ | 12 | :f:`||` | 逻辑或 | +------------------+------------------+------------------+ | 13 | :f:`? :` | 条件运算符 | +------------------+------------------+------------------+ | | :f:`= += -= *=` | | + + + | | 14 | :f:`/= %= &= |=` | 赋值运算符 | + + + | | | :f:`^= <<= >>=` | | +------------------+------------------+------------------+ | 15 | :f:`,` | 逗号运算符 | +------------------+------------------+------------------+ .. note:: 位运算的优先级是:``~`` > ``&`` > ``^`` > ``|`` 。 逻辑运算的优先级是:``!`` > ``&&`` > ``||`` 。 不能重载的运算符:``.`` ,``? :`` ,``sizeof`` ,``.*`` , ``::``。 逻辑与:``exp1 && exp2`` ,如果 ``exp1`` 值为 0,则不对 ``exp2`` 求值。 逻辑或:``exp1 || exp2`` ,如果 ``exp1`` 值为 1,则不对 ``exp2`` 求值。 参考资料 --------- 1. Operator Overloading https://isocpp.org/wiki/faq/operator-overloading