首先看看是怎么定义的
http://en.cppreference.com/w/cpp/language/operator_precedence
The following table lists the precedence and associativity of C++ operators. Operators are listed top to bottom, in descending precedence.
Precedence | Operator | Description | Associativity |
---|---|---|---|
1 | :: | Scope resolution | Left-to-right |
2 | ++ -- | Suffix/postfix increment and decrement | |
() | Function call | ||
[] | Array subscripting | ||
. | Element selection by reference | ||
−> | Element selection through pointer | ||
3 | ++ -- | Prefix increment and decrement | Right-to-left |
+ − | Unary plus and minus | ||
! ~ | Logical NOT and bitwise NOT | ||
(type) | Type cast | ||
* | Indirection (dereference) | ||
& | Address-of | ||
sizeof | Size-of | ||
new , new[] | Dynamic memory allocation | ||
delete , delete[] | Dynamic memory deallocation | ||
4 | .* ->* | Pointer to member | Left-to-right |
5 | * / % | Multiplication, division, and remainder | |
6 | + − | Addition and subtraction | |
7 | << >> | Bitwise left shift and right shift | |
8 | < <= | For relational operators < and ≤ respectively | |
> >= | For relational operators > and ≥ respectively | ||
9 | == != | For relational = and ≠ respectively | |
10 | & | Bitwise AND | |
11 | ^ | Bitwise XOR (exclusive or) | |
12 | | | Bitwise OR (inclusive or) | |
13 | && | Logical AND | |
14 | || | Logical OR | |
15 | ?: | Ternary conditional | Right-to-left |
= | Direct assignment (provided by default for C++ classes) | ||
+= −= | Assignment by sum and difference | ||
*= /= %= | Assignment by product, quotient, and remainder | ||
<<= >>= | Assignment by bitwise left shift and right shift | ||
&= ^= |= | Assignment by bitwise AND, XOR, and OR | ||
16 | throw | Throw operator (for exceptions) | |
17 | , | Comma | Left-to-right |
还有一个问题,就是设计到成员和括号的时候:
如 dgif_lib.c中 有这么一段:
#define READ(_gif,_buf,_len) \
(((GifFilePrivateType*)_gif->Private)->Read ? \
((GifFilePrivateType*)_gif->Private)->Read(_gif,_buf,_len) : \
fread(_buf,1,_len,((GifFilePrivateType*)_gif->Private)->File))
<转>如何记忆两种结合性和15种优先级?下面讲述一种记忆方法。
结合性有两种,一种是自左至右,另一种是自右至左,大部分运算符的结合性是自左至右,只有单目运算符、三目运算符的赋值运算符的结合性自右至左。优先级有15种。记忆方法如下:
记住一个最高的:构造类型的元素或成员以及小括号。
记住一个最低的:逗号运算符。
剩余的是一、二、三、赋值。
意思是单目、双目、三目和赋值运算符。
在诸多运算符中,又分为:
算术、关系、逻辑。
两种位操作运算符中,移位运算符在算术运算符后边,逻辑位运算符在逻辑运算符的前面。再细分如下:
算术运算符分 *,/,%高于+,-。
关系运算符中,》,》=,《,〈=高于==,!=。
逻辑运算符中,除了逻辑求反(!)是单目外,逻辑与(&&)高于逻辑或(||)。
逻辑位运算符中,除了逻辑按位求反(~)外,按位与(&)高于按位半加(^),高于按位或(|)。
这样就将15种优先级都记住了,再将记忆方法总结如下:
去掉一个最高的,去掉一个最低的,剩下的是一、二、三、赋值。双目运算符中,顺序为算术、关系和逻辑,移位和逻辑位插入其中。
(GifFilePrivateType*)_gif->Private 所以这里先有_gif->Private,然后做强制转化--上表中第一大优先级(Type cast)。