 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Benoit Dejean Guest
|
Posted: Wed Feb 11, 2004 10:33 pm Post subject: héritage, redéfinition « relaxed », fonction statique |
|
|
(excusez moi pour le titre qui laisse un peu à désirer)
étant donné cette hiérarchie de classes
struct Shape
{
virtual Shape* clone() const =0;
virtual ~Shape()
{ }
};
struct Foo
: public Shape
{
Foo* clone() const
{
return new Foo(*this);
}
static Foo* create()
{
return new Foo();
}
};
ce morceau de code
Foo foo;
Foo *shape1 = foo.clone();
Shape *shape2 = shape1->clone();
ne pose aucun problème
et celui-là
typedef Foo* (&refFooCreator)();
typedef Foo* (*ptrFooCreator)();
refFooCreator rfoo(Foo::create);
ptrFooCreator pfoo(&Foo::create);
non plus
par contre ce dernier
typedef Shape* (&refShapeCreator)();
typedef Shape* (*ptrShapeCreator)();
refShapeCreator rshape(Foo::create);
ptrShapeCreator pshape(Foo::create);
pose problème, les convertions
'Foo*()() -> Shape*(&)()'
et
'Foo*(*)() -> Shape*(*)()'
n'étant pas définies ce que je comprends parfaitement.
y a t il une erreur de ma part ?
sinon, je trouve cela un peu regrettable, car je suis obligé d'utiliser
des instances de sous-types de Shape, et d'utiliser ptrShape->create()
pour appeler la bonne fonction create (alors définie non-static virtuelle
pure const).
Merci
|
|
| Back to top |
|
 |
Jean-Marc Bourguet Guest
|
Posted: Thu Feb 12, 2004 8:09 am Post subject: Re: héritage, redéfinition « relaxed », fonction statique |
|
|
La conversion Foo* vers Shape* peut s'accompagner d'un changement de
la representation du pointeur (par exemple en cas d'heritage multiple
c'est vrai pour au moins un des deux ancetres), donc passer une
fonction que renvoie un Foo* ou on attends une fonction que renvoie un
Shape* devrait etre accompagne de la transformation attendue, qui
n'est pas connue au lieu d'utilisation.
Dans le cas du retour covariant (Shape::clone et Foo::clone), clone
renvoie toujours un Shape et le compilateur ajoute a l'endroit de
l'appel la transformation necessaire, qui est connue.
A+
--
Jean-Marc
FAQ de fclc++: http://www.cmla.ens-cachan.fr/~dosreis/C++/FAQ
C++ FAQ Lite en VF: http://www.ifrance.com/jlecomte/c++/c++-faq-lite/index.html
Site de usenet-fr: http://www.usenet-fr.news.eu.org
|
|
| Back to top |
|
 |
Benoit Dejean Guest
|
Posted: Thu Feb 12, 2004 10:14 am Post subject: Re: héritage, redéfinition « relaxed », fonction statique |
|
|
Le Thu, 12 Feb 2004 09:09:21 +0100, Jean-Marc Bourguet a écrit :
merci beaucoup pour ton explication.
|
|
| 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
|
|