operators
Aus cppreference.com
<metanoindex/>
Überladen von Operatoren
Syntax
type operator op ( params ) ;
|
|||||||||
Erklärung
- <type> ist / sind die Art (en) der Variablen .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> die jeweiligen Betreiber (zB
+,+=,<<,>>,&&,||,%, 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> ist / sind der Name (n) der erforderlichen Parameter (abhängig vom Netzbetreiber) .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.
Einschränkungen
- Sie können keine neuen Operatoren wie
**oder&|.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. - Nicht alle Operatoren können überladen werdenOriginal: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. - Einige Betreiber kann nur als nicht statische Klasse Mitglieder überlastet werdenOriginal: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. - Kurzschluss-Auswertung nicht mit überladenen Operatoren arbeitenOriginal: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.
Operator Anrufe
Überladene Operatoren Aufruf erfolgt über den üblichen Infix-Notation werden
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
oder eine Funktion-ähnlichen Notation
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)
Beispiel
#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
