Original Post
Hi! I'm having a little problem, and after some googleing, I can't seem to find a decent solution... (btw, I'm new here, and not sure if this belongs in the beginners forum or not, feel free to move it there if it does (?)) I've got a basic factory / instance design for a set of classes. for example: now, I'd like to specialize the two virtual functions "VirtualInstanciate()" and "ParentFactory()", making their return type covariant: however, simply forward-declaring "CDuckFactory" doesn't cut it, as it would seem the compiler needs derivation information to be sure the return type is actually covariant (which seems pretty logical), and it doesn't look too happy when given something like: "class CDuckFactory : public CAnimalFactory;" the easy way would be to ignore co-variance, but it's a bit annoying, and not very user-friendly for the users of these classes (and pretty ugly, having to carry casts all over the place...) in the worst case scenario I'll just go with not using covariance for the "VirtualInstanciate" method, but still... isn't there a clean way to do this ? [Edited by - momotte on December 18, 2007 6:08:06 PM]
class CAnimalFactory; // forward declare... that works OK for the base class..
class CAnimalInstance
{
private:
public:
CAnimalInstance() {}
~CAnimalInstance() {}
virtual const CAnimalFactory *ParentFactory() const { return NULL; }
};
class CAnimalFactory
{
protected:
virtual CAnimalInstance *VirtualInstanciate();
public:
CAnimalFactory() {}
~CAnimalFactory() {}
CAnimalInstance *Instanciate() { return VirtualInstanciate(); }
};
class CDuckFactory; // not enough, we need derivation information too
class CDuckInstance : public CAnimalInstance
{
private:
const CDuckFactory *m_ParentFactory;
public:
CDuckInstance() {}
~CDuckInstance() {}
virtual const CDuckFactory *ParentFactory() const { return m_ParentFactory; }
};
class CDuckFactory : public CAnimalFactory
{
protected:
virtual CDuckInstance *VirtualInstanciate();
public:
CDuckFactory() {}
~CDuckFactory() {}
};