c++优先级(来自http://www.cppreference.com)
2012-04-21 10:07
369 查看
原文地址
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.
When parsing an expression, an operator which is listed on some row will be bound tighter (as if by parentheses) to its arguments than any operator that is listed on a row further below it.
For example, the expressions std::cout<<a&b and *p++ are
parsed as(std::cout<<a)&b and *(p++),
and not as std::cout<<(a&b) or (*p)++.
Operators that are in the same cell (there may be several rows of operators listed in a cell) are evaluated with the same precedence, in the given direction. For example, the expression a=b=c is
parsed as a=(b=c),
and not as (a=b)=c because
of right-to-left associativity.
An operator's precedence is unaffected by overloading.
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 | ||
typeid() | Run-time type information (see typeid) | ||
const_cast | Type cast (see const_cast) | ||
dynamic_cast | Type cast (see dynamic_cast) | ||
reinterpret_cast | Type cast (see reinterpret_cast) | ||
static_cast | Type cast (see static_cast) | ||
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 |
16 | = | 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 | ||
17 | throw | Throw operator (exceptions throwing) | |
18 | , | Comma | Left-to-right |
For example, the expressions std::cout<<a&b and *p++ are
parsed as(std::cout<<a)&b and *(p++),
and not as std::cout<<(a&b) or (*p)++.
Operators that are in the same cell (there may be several rows of operators listed in a cell) are evaluated with the same precedence, in the given direction. For example, the expression a=b=c is
parsed as a=(b=c),
and not as (a=b)=c because
of right-to-left associativity.
An operator's precedence is unaffected by overloading.
相关文章推荐
- 文件操作 C++ ;;http://www.cppblog.com/niewenlong/archive/2007/06/14/26302.aspx
- 如何检查C++中的内存泄漏 (原文地址:http://www.cppblog.com/Lyt/archive/2009/03/22/77517.html)
- 工欲善其事,必先利其器——VC2005的常用快捷键(来自http://www.cppblog.com/corelito/archive/2008/10/17/64233.html)
- C++中引用(&)(来自http://www.cnblogs.com/Mr-xu/archive/2012/08/07/2626973.html)
- asp.net(C#)调用C++程序并交互操作【转】http://www.cnblogs.com/greatverve/archive/2010/06/07/Csharp-transfer-Cpp.html
- equal http://www.cplusplus.com/reference/algorithm/equal/
- http://blog.csdn.net/waterpub和http://www.cppblog.com/jinglexy博客转移至此
- 我的新地址 http://www.cppblog.com/flyingxu/
- 模版详解(模版与宏) 转自:http://www.cppblog.com/zmllegtui/archive/2008/10/28/65316.html
- Referenced file contains error(http://www.ibatis.com/dtd/sql-map-2.dtd) 错误
- error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": http://landinghub.visualstudio.com/visual-cpp-build-tools
- http://www.cppblog.com/flyinghearts/archive/2012/03/18/168270.html
- Windows Mobile 发送短信的问题(转自http://www.cppblog.com/SpringSnow/archive/2009/06/08/76107.html)
- 转换汉字为unicode形式的字符串和转换unicode形式字符串转换成汉字__http://www.cppblog.com/biao/archive/2010/12/21/137087.html
- http://www.cppblog.com/twzheng/archive/2008/07/07/55563.html
- 该Blog停止更新,新的blog地址http://www.cppblog.com/zuroc/
- error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": http://landinghub.visualstudio.com/visual-cpp-build-tools
- PostThreadMessage http://www.cppblog.com/sandy/archive/2005/12/31/2320.html
- C++ 类的静态成员详细讲解 http://www.cnblogs.com/morewindows/archive/2011/08/26/2154198.html
- C++:STL标准入门汇总(http://www.cnblogs.com/shiyangxt/archive/2008/09/11/1289493.html)