来自:http://www.learncpp.com/cpp-tutorial/31-precedence-and-associativity/
为了正确的计算类似下面这样的式子,4 + 2 * 3,我们必须知道操作符做了什么,以及它们执行的顺序,也就是说首先要明白操作符的优先级。使用常规的数学上的优先级规则,我们知道上面的式子可以这么求解 4 + (2 * 3) = 10
在C++中,所有的操作符都分配有自己的优先级。具有高优先级的步骤先被计算。你可以从下面的表中看出乘法与除法(优先级序列5)比加减法(优先级序列6)高。编译器通过这些标签确定如何对它遇到的算式求解。
如果两个操作符具有相同的优先级序列,并在表达式中相邻,结合规则告诉编译器是否从左向右或从右向左进行求值。比如,3*4/2,乘法和除法都是在等级5.具有从左向右的结合性,因此表达式可以被解释为(3*4)/2=6.
Prec/Ass | Operator | Description | Example |
---|---|---|---|
1 None | :: :: | Global scope (unary) Class scope (binary) | ::g_nGlobalVar = 5; Class::m_nMemberVar = 5; |
2 L->R | () () () [] . -> ++ –– typeid const_cast dynamic_cast reinterpret_cast static_cast | Parenthesis Function call Implicit assignment Array subscript Member access from object Member access from object ptr Post-increment Post-decrement Run-time type information Cast away const Run-time type-checked cast Cast one type to another Compile-time type-checked cast | (x + y) * 2; Add(x, y); int nValue(5); aValue[3] = 2; cObject.m_nValue = 4; pObject->m_nValue = 4; nValue++; nValue––; typeid(cClass).name(); const_cast<int*>(pnConstValue); dynamic_cast<Shape*>(pShape); reinterpret_cast<Class2>(cClass1); fValue = static_cast<float>(nValue); |
3 R->L | + - ++ –– ! ~ (type) sizeof & * new new[] delete delete[] | Unary plus Unary minus Pre-increment Pre-decrement Logical NOT Bitwise NOT C-style cast Size in bytes Address of Dereference Dynamic memory allocation Dynamic array allocation Dynamic memory deletion Dynamic array deletion | nValue = +5;M nValue = -1; ++nValue; ––nValue; if (!bValue) nFlags = ~nFlags; float fValue = (float)nValue; sizeof(int); address = &nValue; nValue = *pnValue; int *pnValue = new int; int *panValue = new int[5]; delete pnValue; delete[] panValue; |
4 L->R | ->* .* | Member pointer selector Member object selector | pObject->*pnValue = 24; cObject->.*pnValue = 24; |
5 L->R | * / % | Multiplication Division Modulus | int nValue = 2 * 3; float fValue = 5.0 / 2.0; int nRemainder = 10 % 3; |
6 L->R | + - | Addition Subtraction | int nValue = 2 + 3; int nValue = 2 – 3; |
7 L->R | << >> | Bitwise shift left Bitwise shift right | int nFlags = 17 << 2; int nFlags = 17 >> 2; |
8 L->R | < <= > >= | Comparison less than Comparison less than or equals Comparison greater than Comparison greater than or equals | if (x < y) if (x <= y) if (x > y) if (x >= y) |
9 L->R | == != | Equality Inequality | if (x == y) if (x != y) |
10 L->R | & | Bitwise AND | nFlags = nFlags & 17; |
11 L->R | ^ | Bitwise XOR | nFlags = nFlags ^ 17; |
12 L->R | | | Bitwise OR | nFlags = nFlags | 17; |
13 L->R | && | Logical AND | if (bValue1 && bValue2) |
14 L->R | || | Logical OR | if (bValue1 || bValue2) |
15 L->R | ?: | Arithmetic if | return (x < y) ? true : false; |
16 R->L | = *= /= %= += -= <<= >>= &= |= ^= | Assignment Multiplication assignment Division assignment Modulus assignment Addition assignment Subtraction assignment Bitwise shift left assignment Bitwise shift right assignment Logical AND assingment Logical OR assignment Logical XOR assignment | nValue = 5; nValue *= 5; fValue /= 5.0; nValue %= 5; nValue += 5; nValue -= 5; nFlags <<= 2; nFlags >>= 2; nFlags &= 17; nFlags |= 17; nFlags ^= 17; |
17 L->R | , | Comma operator | iii++, jjj++, kkk++; |
随着章节的深入,对于上面操作符的认识也会越来越多的。