Surchage opérateur & instanciation dynamique
Bonjour,
Je voudrais surcharger l'opérateur + et faire la somme de deux objets instanciés dynamiquement mais ça ne marche pas.
Ci-dessous : le premier cas fonctionne, le second retourne à la compilation :
" invalid operands of types `Som*' and `Som*' to binary `operator+'"
Pouvez vous m'éclairer?
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
#include <iostream>
using namespace std;
class Som
{
int value;
public:
Som(int tmpInt):value(tmpInt){}
int getValue(void) {return value;}
int operator + (Som tmpSom) {int res = value + tmpSom.getValue();return res;}
};
int main()
{
Som a(5);
Som b(6);
cout << "som = " << a+b;
return 0;
} |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
#include <iostream>
using namespace std;
class Som
{
int value;
public:
Som(int tmpInt):value(tmpInt){}
int getValue(void) {return value;}
int operator + (Som *tmpSom) {int res = value + tmpSom->getValue();return res;}
};
int main()
{
Som *a = new Som(5);
Som *b = new Som(6);
cout << "som = " << a+b;
return 0;
} |