operators
![]() | This page has been machine-translated from the English version of the wiki using Google Translate. The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
Sommaire |
[modifier]Surcharge d'opérateurs
[modifier]Syntaxe
typeoperator op( params) ; | |||||||||
[modifier]Explication
- <type> est / sont du type (s) des variables .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> est l'opérateur particulier (par exemple
+
,+=
,<<
,>>
,&&
,||
,%
, 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> est / sont le nom (s) des paramètres requis (dépend de l'opérateur) .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.
[modifier]Restrictions
- Vous ne pouvez pas créer de nouveaux opérateurs tels que
**
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. - Pas tous les opérateurs peuvent être surchargésOriginal: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. - Certains opérateurs ne peut être surchargé en tant que membres de classe non statiqueOriginal: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. - Court-circuit d'évaluation ne fonctionne pas avec des opérateurs surchargésOriginal: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.
[modifier]Les appels de l'opérateur
You can help to correct and verify the translation. Click here for instructions.
a+b
You can help to correct and verify the translation. Click here for instructions.
operator+(a,b)
[modifier]Exemple
#include <iostream>usingnamespace 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 namefriend 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; return0;}
Résultat :
3/8 3 1/2
[modifier]Voir aussi
Opérateurs ordinaires | ||||||
---|---|---|---|---|---|---|
affectation | incrémentation décrémentation | arithmétique | logique | comparaison | accès aux membre | autre |
a = b | ++a | +a | !a | a == b | a[b] | a(...) |
Opérateurs spéciaux | ||||||
static_cast convertit un type dans un autre type compatible dynamic_cast convertit une classe de base virtuelle dans une classe dérivée const_cast convertit un type dans un type compatible avec des cv-qualifiers différents reinterpret_cast convertit un type dans un type incompatibles new allocation de la mémoire delete libère de la mémoire sizeof récupère la taille d'un type sizeof... récupère la taille d'un paquet de paramètres (depuis C++11) typeid récupère les informations de type d'un type noexcept vérifie si une expression peut lancer une exception (depuis C++11) alignof récupère les conditions d'alignement d'un type (depuis C++11) |