operators - cppreference.com
De cppreference.com
<metanoindex/>
Sobrecarga de operador
Sintaxe
type operator op ( params ) ;
|
|||||||||
Explicação
<type> é / são o (s) das variáveis .
Original:
<type> is/are the type(s) of the variables.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.<op> é o operador particular (por exemplo
+,+=,<<,>>,&&,||,%, etc).Original:
<op> is the particular operator (e.g.
+,+=,<<,>>,&&,||,%, etc.).The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.<params> é / são o (s) nome dos parâmetros necessários (depende da operadora).
Original:
<params> is/are the name(s) of the required parameters (depends on the operator).
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Restrições
Você não pode criar novos operadores, como
**ou&|.Original:
You cannot create new operators such as
**or&|.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.Nem todos os operadores podem ser sobrecarregados
Original:
Not all operators can be overloaded
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.Algumas operadoras só pode ser sobrecarregado como membros não-estáticos da classe
Original:
Some operators can only be overloaded as non-static class members
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.Avaliação curto-circuito não funciona com operadores sobrecarregados
Original:
Short-circuit evaluation doesn't work with overloaded operators
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Convida operador
Operadores sobrecarregados podem ser chamadas usando a notação infixa habitual
Original:
Overloaded operators can be called using the usual infix notation
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
ou uma função semelhante a notação
Original:
or a function-like notation
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Exemplo
#include <iostream> using namespace std; class Fraction{ private: int numerator, denominator; public: Fraction(int n, int d): numerator(n), denominator(d) {} // Note that the keyword operator combined with an actual // operator is used as the function name friend ostream& operator<<(ostream&, Fraction&); }; ostream& operator<<(ostream& out, Fraction& f){ out << f.numerator << '/' << f.denominator; return out; } int main(){ Fraction f1(3, 8); Fraction f2(1, 2); cout << f1 << endl; cout << 3 << ' ' << f2 << endl; return 0; }
Saída:
Consulte também
| Operadores comuns | ||||||
|---|---|---|---|---|---|---|
| assinamento | incremento descremento |
aritmético | lógico | comparação | acesso de membro |
outros |
|
|
|
|
|
|
|
|
| Operadores Especiais | ||||||
|
static_cast converte um tipo a um outro tipo relacionado | ||||||