A tabela a seguir mostra a precedência ea associatividade dos operadores C + +. Operadores são listados de cima para baixo, em ordem decrescente de precedência.
Original:
The following table lists the precedence and associativity of C++ operators. Operators are listed top to bottom, in descending precedence.
The text has been machine-translated via
Google Translate.
You can help to correct and verify the translation. Click
here for instructions.
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 |
---|
Ao analisar uma expressão, um operador que está listado em alguma linha será obrigado apertado (como se por parênteses) para os seus argumentos do que qualquer operador que está listado em uma linha mais abaixo. Por exemplo, as expressões e
std::cout<<a&b*p++ são analisados como
(std::cout<<a)&b e
*(p++), e não como
std::cout<<(a&b) ou
(*p)++.
Original:
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)++.
The text has been machine-translated via
Google Translate.
You can help to correct and verify the translation. Click
here for instructions.
Os operadores que estão na mesma célula (pode haver diversas fileiras dos operadores listados na célula) são avaliadas com a mesma precedência, na direcção dada. Por exemplo, a expressão
a=b=c é analisado como
a=(b=c), e não como
(a=b)=c devido a direita para a esquerda associatividade.
Original:
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.
The text has been machine-translated via
Google Translate.
You can help to correct and verify the translation. Click
here for instructions.
Precedência de um operador não é afetado pela sobrecarga.
Original:
An operator's precedence is unaffected by overloading.
The text has been machine-translated via
Google Translate.
You can help to correct and verify the translation. Click
here for instructions.
A norma não especifica níveis de precedência. Eles são derivados a partir da gramática.
Original:
The standard itself doesn't specify precedence levels. They are derived from the grammar.
The text has been machine-translated via
Google Translate.
You can help to correct and verify the translation. Click
here for instructions.
const_cast,
static_cast,
dynamic_cast,
reinterpret_cast e
typeid não estão incluídos, uma vez que nunca são ambíguos.
Original:
The text has been machine-translated via
Google Translate.
You can help to correct and verify the translation. Click
here for instructions.
Alguns dos operadores têm
grafias alternativas (por exemplo, para
and&&
,
or para
||
,
not para
!
, etc).
Original:
The text has been machine-translated via
Google Translate.
You can help to correct and verify the translation. Click
here for instructions.
[editar]Veja também
Ordem de avaliação dos argumentos do operador em tempo de execução.
Original:
The text has been machine-translated via
Google Translate.
You can help to correct and verify the translation. Click
here for instructions.
Operadores comuns |
---|
assinamento | incremento descremento | aritmético | lógico | comparação | acesso de membro | outros |
a = b a += b a -= b a *= b a /= b a %= b a &= b a |= b a ^= b a <<= b a >>= b | ++a --a a++ a-- | +a -a a + b a - b a * b a / b a % b ~a a & b a | b a ^ b a << b a >> b | !a a && b a || b | a == b a != b a < b a > b a <= b a >= b a <=> b | a[b] *a &a a->b a.b a->*b a.*b | a(...) a, b ?: |
Operadores Especiais |
---|
static_cast converte um tipo a um outro tipo relacionado dynamic_cast converte dentro de hierarquias de herança const_cast adiciona ou remove qualificadores cv reinterpret_cast converte tipo a tipo não relacionado C-style cast converte um tipo a um outro por uma mistura de static_cast , const_cast , e reinterpret_cast new cria objetos com duração de armazenamento dinâmico delete destrói objetos anteriormente criads pela expressão new e libera área de memória obtida sizeof pesquisa o tamanho de um tipo sizeof... pesquisa o tamanho de um pacote de parâmetro(desde C++11) typeid pesquisa a informação de tipo de um tipo noexcept checa se uma expressão pode lançar uma exceção (desde C++11) alignof pesquisa requerimentos de alinhamento de um tipo (desde C++11) |