Bonjour !
Voici le code qui me pose un problème :
Ce code fonctionne très bien en l'état. Mon problème est que je voudrais redéfinir dans "Child" la méthode "func" issu de l'héritage "Mother<int>".Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 #include <iostream> using namespace std; template <class T> struct Mother { void func(T val) { cout << val << endl; } }; struct Child : public Mother<int>, public Mother<double> { }; int main(int argc, char** argv) { Child c; c.Mother<int>::func(314); return 0; };
Si je fais...
... ça compile très bien mais ça ne fait pas ce que je veux car, en fait, cela ne redéfinit pas la méthode "func" issu de l'héritage "Mother<int>". Ainsi, si j'appelle "c.Mother<int>::func(314)" dans la fonction "main", seule la méthode "func" de la classe de base "Mother<int>" est appelée !Code:
1
2
3
4
5
6
7 struct Child : public Mother<int>, public Mother<double> { void func(int val) { cout << "Fonctionnalité supplémentaire" << endl; Mother<int>::func(val); } };
En fait, je voudrais faire quelque chose dont la syntaxe pourrait être...
... malheureusement, cette syntaxe n'est pas acceptée par les compilateurs :Code:
1
2
3
4
5
6
7 struct Child : public Mother<int>, public Mother<double> { void Mother<int>::func(int val) { cout << "Fonctionnalité supplémentaire" << endl; Mother<int>::func(val); } };
G++ me dit : "error: cannot declare member function `Mother<int>::func' within `Child'"
et Visual C++ : "error C3240: 'func' : must be a non-overloaded abstract member function of 'Mother<T>'"
Merci pour vos réponses !