Chapitre 7 : Surcharge d'opérateur

On peut sercharger certains opérateur (++, +, /, ...) Si @ est un opérateur on peut le surcharger avec :

a@b \rightarrow operator@(a,b);

@a \rightarrow operator@(a);

a@ \rightarrow operator@(a,0); //le deuxieme argument est int

Exemple

class A{
  int atr = 0;
  public:
    A somme(A obj){
    A r;
    r.atr = obj.atr + this->atr;
    return r;
  }
};

A operator+(A og, A od){
  return og.somme(od);
}

int main(void){
  A obj1;
  A obj2;
  A res = obj1.somme(obj2);
  A res2 = obj1 + obj2;
  //A res2 = operator(obj1, obj2);
  return 0;
}