Espaces de noms
Variantes
Affichages
Actions

operators

De cppreference.com.

 
 
Langage C++
Sujets généraux
Contrôle de flux
Instructions conditionnelles
Instructions d'itération
Instructions de saut
Fonctions
déclaration de fonction
expression lambda
fonction générique
spécificateur inline
spécification d'exception (obsolète)
spécificateur noexcept (C++11)
Exceptions
Espaces de noms
Types
spécificateur decltype (C++11)
Qualificatifs
qualificatifs const et volatile
qualificatifs de stockage
qualificatif constexpr (C++11)
qualificatif auto (C++11)
qualificatif alignas (C++11)
Initialisation
Littéraux
Expressions
opérateurs alternatifs
Utilitaires
Types
déclaration typedef
déclaration d'alias de type (C++11)
attributs (C++11)
Jette
Original:
Casts
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
conversions implicites
conversion const_cast
conversion static_cast
conversion dynamic_cast
conversion reinterpret_cast
conversions style C et style fonction
Allocation de mémoire
Classes
Qualificatifs spécifiques aux membres de classe
Fonctions membres spéciales
Modèles
classes génériques
fonctions génériques
spécialisation de modèles
paquets de paramètres (C++11)
Divers
Assembleur
 

Sommaire

[modifier] Surcharge d'opérateurs

[modifier] Syntaxe

type operator 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és
    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.
  • Certains opérateurs ne peut être surchargé en tant que membres de classe non statique
    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.
  • Court-circuit d'évaluation ne fonctionne pas avec des opérateurs surchargés
    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.

[modifier] Les appels de l'opérateur

Opérateurs surchargés peuvent être appelées en utilisant la notation infixe d'habitude
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.
a+b
ou une fonction semblable à la 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.
operator+(a,b)

[modifier] Exemple

#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;
 }

Résultat :

3/8
3 1/2

[modifier] Voir aussi

Common operators
affectation incrémentation décrémentation arithmétique logique comparaison accès aux membre autre

a = b
a = rvalue
a += b
a -= b
a *= b
a /= b
a %= b
a &= b
a |= b
a ^= b
a <<= b
a >>= b

++a
--a
a++
a--

+a
-a
a + b
a - b
a * b
a / b
a % b
~a
a & b
a | b
a ^ b
a << b
a >> b

!a
a && b
a || b

a == b
a != b
a < b
a > b
a <= b
a >= b

a[b]
*a
&a
a->b
a.b
a->*b
a.*b

a(...)
a, b
(type) a
? :

Opérateurs spéciaux
static_cast convertit un type à un autre
type compatible
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 convertit classe de base virtuelle à class
dérivée
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 convertit un type à l'autre compatible avec cv qualifiers
Original:
const_cast converts type to compatible type with different cv qualifiers
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
reinterpret_cast convertit type type
incompatibles
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 alloue 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 libère 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 interroge la taille d'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... interroge la taille d'un Paquet de paramètre (depuis C++11)
Original:
sizeof... queries the size of a Paquet de paramètre (depuis C++11)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
typeid interroge les informations de type d'une 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 vérifie si une expression peut jeter une exception (depuis C++11)
Original:
noexcept checks if an expression can throw an exception (depuis C++11)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
alignof requêtes exigences d'alignement d'un (depuis C++11) type
Original:
alignof queries alignment requirements of a type (depuis C++11)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.