 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Vincent Richard Guest
|
Posted: Fri Feb 06, 2004 6:51 pm Post subject: Héritage multiple et fonction virtuelle pure |
|
|
Bonsoir,
Soit le cas d'héritage multiple suivant :
// interfaces
struct a
{
virtual int f() = 0; // ligne 7
};
struct b : public a
{
};
// une implémentation
struct A : public a
{
int f() { return 1234; }
};
struct B : public b, public A
{
};
int main()
{
b* myB = new B; // ligne 29
}
A la compilation, le compilateur indique :
hm.cpp: Dans function « int main() »:
hm.cpp:29: error: cannot allocate an object of type `B'
hm.cpp:29: error: because the following virtual functions are abstract:
hm.cpp:7: error: virtual int a::f()
Il ne devrait pourtant pas y avoir besoin de réimplémenter f() une "seconde"
fois puisqu'un B est aussi un A, je me trompe ?
Comment puis-je résoudre facilement ce problème tout en gardant mon schéma
d'héritage ? (raison : un 'B' est un 'A' qui contient d'autres 'A', et 'a'
et 'b' sont les interfaces respectives de 'A' et 'B'.)
Merci d'avance pour vos réponses.
Vincent
--
vmime, une bibliothèque C++ sous licence GPL pour parser et générer
des messages au format MIME : http://www.sourceforge.net/projects/vmime/
|
|
| Back to top |
|
 |
Alexandre Guest
|
Posted: Fri Feb 06, 2004 8:05 pm Post subject: Re: Héritage multiple et fonction virtuelle pure |
|
|
voici la solution :
struct a
{
virtual int f() = 0; // ligne 7
};
struct b : virtual public a
{
};
// une implémentation
struct A : virtual public a
{
int f() { return 1234; }
};
struct B : public b, public A
{
};
int main()
{
b* myB = new B; // ligne 29
}
la différence ? L'ancêtre commun 'a' est hérité en *virtuel* pour A et b.
Donc le membre int f() n'est présent qu'une seule fois. Donc c'est A::f qui
est utilisée pour B::f, sinon, dans ton cas, B possédait *deux* méthodes f,
A::f et b::f; donc une virtuelle pure...
Pénible, l'héritage multiple, non ?
|
|
| Back to top |
|
 |
Vincent Richard Guest
|
Posted: Fri Feb 06, 2004 8:10 pm Post subject: Re: Héritage multiple et fonction virtuelle pure |
|
|
Le Vendredi 6 Février 2004 21:05, Alexandre a écrit :
| Quote: | voici la solution :
struct a
{
virtual int f() = 0; // ligne 7
};
struct b : virtual public a
{
};
// une implémentation
struct A : virtual public a
{
int f() { return 1234; }
};
struct B : public b, public A
{
};
|
Je venais juste de trouver la réponse en consultant la C++ FAQ Lite :
http://www.parashift.com/c++-faq-lite/multiple-inheritance.html#faq-25.8
| Quote: | la différence ? L'ancêtre commun 'a' est hérité en *virtuel* pour A et b.
Donc le membre int f() n'est présent qu'une seule fois. Donc c'est A::f
qui est utilisée pour B::f, sinon, dans ton cas, B possédait *deux*
méthodes f, A::f et b::f; donc une virtuelle pure...
|
Merci pour l'explication.
Vincent
--
vmime, une bibliothèque C++ sous licence GPL pour parser et générer
des messages au format MIME : http://www.sourceforge.net/projects/vmime/
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|