On peut sercharger certains opérateur (++, +, /, ...) Si @ est un opérateur on peut le surcharger avec :
operator@(... , ...) //binaire infixe, unaire suffixeoperator@(...); //unaire préfixe (&,!,++,--,...)a@b operator@(a,b);
@a operator@(a);
a@ operator@(a,0); //le deuxieme argument est int
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;
}