operators
Da cppreference.com.
![]() |
Questa pagina è stata tradotta in modo automatico dalla versione in ineglese della wiki usando Google Translate.
La traduzione potrebbe contenere errori e termini strani. Muovi il puntatore sopra al testo per vedere la versione originale. Puoi aiutarci a correggere gli gli errori. Per ulteriori istruzioni clicca qui. |
Indice |
[modifica] L'overload degli operatori
[modifica] Sintassi
type operator op ( params ) ;
|
|||||||||
[modifica] Spiegazione
- <tipo> è / sono il tipo (s) delle variabili.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> è l'operatore particolare (ad esempio
+
,+=
,<<
,>>
,&&
,||
,%
, ecc).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> è / sono il nome (s) i parametri necessari (dipende dal gestore).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.
[modifica] Restrizioni
- Non è possibile creare nuovi operatori come
**
o&|
.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. - Non tutti gli operatori possono essere sottoposti a overloadOriginal:Not all operators can be overloadedThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - Alcuni operatori possono essere sottoposti a overload come membri non statici della classeOriginal:Some operators can only be overloaded as non-static class membersThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - Corto circuito di valutazione non funziona con operatori in overloadOriginal:Short-circuit evaluation doesn't work with overloaded operatorsThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
[modifica] Operatore Chiamate
Operatori di overload può essere chiamato con la notazione infissa solito
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.
You can help to correct and verify the translation. Click here for instructions.
a+b
o di una funzione-notazione
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.
You can help to correct and verify the translation. Click here for instructions.
operator+(a,b)
[modifica] Esempio
#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; }
Output:
3/8 3 1/2
[modifica] Vedere anche
Common operators | ||||||
---|---|---|---|---|---|---|
assegnazione | incrementNJdecrement | aritmetica | logico | confronto | memberNJaccess | altra |
a = b |
++a |
+a |
!a |
a == b |
a[b] |
a(...) |
Special operators | ||||||
static_cast converte un tipo a un altro
tipo compatibile Original: static_cast converts one type to another compatible type The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. dynamic_cast converte classe virtuale di base per class
derivato Original: dynamic_cast converts virtual base class to derived class The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. const_cast converte il tipo di tipo compatibile con diversi cv qualifiers
Original: The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. reinterpret_cast converte tipo type
incompatibile Original: reinterpret_cast converts type to incompatible type The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. new alloca memory
Original: new allocates memory The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. delete dealloca memory
Original: delete deallocates memory The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. sizeof interroga la dimensione di un type
Original: sizeof queries the size of a type The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. sizeof... interroga le dimensioni di un parametro confezione (dal C++11)
Original: The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. typeid interroga le informazioni sul tipo di una type
Original: typeid queries the type information of a type The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. noexcept controlla se un'espressione può lanciare una (dal C++11)
un'eccezione Original: The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. alignof query requisiti di allineamento di un (dal C++11) tipo
Original: alignof queries alignment requirements of a type (dal C++11) The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |