Translations of this page?:

Precedência de Operadores em C++

Os operadores no topo dessa lista são avaliados primeiramente. Operadores dentro de um grupo tem a mesma precedência. Todos operadores são associados da esquerda para a direita a não ser que seja notado.


Precedência Operador Descrição Exemplo Overloadable Associação
1::Operador de resolução de escopoClass::idade = 2;nãonenhuma
2()
()
[]
->
.
++
--
Chamada de função
Inicialização de membro
Acesso a array
Acesso de membro em um ponteiro
Acesso de membro em um objeto
Incrementar(sufixo)
Diminuição(sufixo)
isdigit('1')
c_tor(int x, int y) : _x(x), _y(y*10){};
array[4] = 2;
ptr->age = 34;
obj.age = 34;
for( int i = 0; i < 10; i++ ) cout << i;
for( int i = 10; i > 0; i-- ) cout << i;
sim
sim
sim
sim
não
sim
sim
esquerda para a direita
3!
not
~
compl
++
--
-
+
*
&
new
new []
delete
delete []
(type)
Negação lógica
Outro maneira de chamar !
Bitwise complement
Outro maneira de chamar ~
Incrementar(prefixo)
Diminuição(prefixo)
Menos unário
Mais unário
Deferência
Endereço de…
Alocação e memória dinâminca
Alocação de memória dinâmica para array
Liberação de memória
Liberação de memória para array
Cast para determinado tipo
if( !done ) …

flags = ~flags;

for( i = 0; i < 10; ++i ) cout << i;
for( i = 10; i > 0; --i ) cout << i;
int i = -1;
int i = +1;
int data = *intPtr;
int *intPtr = &data;
long *pVar = new long;
MyClass *ptr = new MyClass(args);
delete pVar;
delete [] array;
int i = (int) floatNum;
sim

sim

sim
sim
sim
sim
sim
sim
sim
sim
sim
sim
sim
direita para esquerda
4->*
.*
Selecionador de membro de ponteiro
Selecionador de membro de objeto
ptr->*var = 24;
obj.*var = 24;
sim
não
esquerda para direita
5*
/
%
Multiplicação
Divisão
Módulo
int i = 2 * 4;
float f = 10.0 / 3.0;
int rem = 4 % 3;
sim
sim
sim
esquerda para direita
6+
-
Adição
Subtração
int i = 2 + 3;
int i = 5 - 1;
sim
sim
esquerda para direita
7<<
>>
Bitwise shift à esquerda
Bitwise shift à direita
int flags = 33 << 1;
int flags = 33 >> 1;
sim
sim
esquerda para direita
8<
<=
>
>=
Comparação menor-que
Comparação maior-ou-igual-que
Comparação maior-que
Comparação maior-ou-igual-que
if( i < 42 ) …
if( i <= 42 ) ...
if( i > 42 ) …
if( i >= 42 ) ...
sim
sim
sim
sim
esquerda para direita
9==
eq
!=
not_eq
Comparação igual a
Outra maneira de chamar ==
Comparação diferente de
Outra maneira de chamar !=
if( i == 42 ) ...

if( i != 42 ) …
sim
-
sim
left to right
10&
bitand
Bitwise AND
Outra maneira de chamar &
flags = flags & 42;
sim
esquerda para direita
11^
xor
Bitwise exclusive OR (XOR)
Outra maneira de chamar ^
flags = flags ^ 42;
sim
esquerda para direita
12|
bitor
Bitwise inclusive (normal) OR
Outra maneira de chamar |
flags = flags | 42;
sim
esquerda para direita
13&&
and
AND lógico
Outra maneira de chamar &&
if( conditionA && conditionB ) …
sim
esquerda para direita
14||
or
OR lógico
Outra maneira de chamar ||
if( conditionA || conditionB ) ...
sim
esquerda para direita
15? :Operador ternário (if-then-else)int i = (a > b) ? a : b;nãodireita para esquerda
16=
+=
-=
*=
/=
%=
&=
and_eq
^=
xor_eq
|=
or_eq
<<=
>>=
Assignment operator
Increment and assign
Decrement and assign
Multiply and assign
Divide and assign
Modulo and assign
Bitwise AND and assign
Alternate spelling for &=
Bitwise exclusive or (XOR) and assign
Alternate spelling for ^=
Bitwise normal OR and assign
Alternate spelling for |=
Bitwise shift left and assign
Bitwise shift right and assign
int a = b;
a += 3;
b -= 4;
a *= 5;
a /= 2;
a %= 3;
flags &= new_flags;

flags ^= new_flags;

flags |= new_flags;

flags <<= 2;
flags >>= 2;
sim
sim
sim
sim
sim
sim
sim

sim

sim

sim
sim
direita para esquerda
17throwLançar exceçãothrow EClass(“Message”);não
18,Sequential evaluation operatorfor( i = 0, j = 0; i < 10; i++, j++ ) …simesquerda para direita

Nota: Por serem funções e não operadores, os seguintes foram removidos: const_cast, dynamic_cast, reinterpret_cast, static_cast, typeid, sizeof.

Order of Evaluation and of Side Effects

One important aspect of C++ that is related to operator precedence is the order of evaluation and the order of side effects in expressions. In some circumstances, the order in which things happen is not defined. For example, consider the following code:

    float x = 1;
    x = x / ++x;

The value of x is not guaranteed to be consistent across different compilers, because it is not clear whether the computer should evaluate the left or the right side of the division first. Depending on which side is evaluated first, x could take a different value.

Furthermore, while ++x evaluates to x+1, the side effect of actually storing that new value in x could happen at different times, resulting in different values for x.

The bottom line is that expressions like the one above are horribly ambiguous and should be avoided at all costs. When in doubt, break a single ambiguous expression into multiple expressions to ensure that the order of evaluation is correct.

Overloading of Operators

Overloading of operators can be very useful and very dangerous. On one hand overloading operators for a class you have created can help with logistics and readability of code. On the other hand you can overload an operator in such a way that it can either obfuscate or just downright break your program. Use carefully.

There are two ways to over load an operator: global function or class member.

Example of overloading with a global function:

     ostream & operator<< (ostream & os, const myClass & rhs);

But to be able to reach any private data within a user defined class you have to declare the global function as a friend within the definition of the class.

Example:

     class myClass {
 
       // Gives the operator<< function access to 'myData'
       // (this declaration should not go in public, private or protected)
       friend ostream & operator<< (ostream & lhs, const myClass & rhs);
 
       private:
         int myData;
     }

Overloading with a class member can be done as follows:

     class myClass {
 
       public:
         // The left hand side of this operator is  a pointer to 'this'.
         int operator+ (const myClass & rhs);
 
       private:
         int myData;
     }
 
• • • SitemapRecent changesRSScc