I have defined a Cloneable interface:
struct Cloneable
{
virtual Cloneable * clone(void) const = 0;
}
I have also some other interface classes (content not relevant to issue):
struct Interface
{
};
struct Useful_Goodies
{
};
I have created a leaf object which inherits from the above classes:
struct Leaf : public Cloneable, public Interface, public Useful_Goodies
{
Leaf * clone(void) const // Line #1 for discussion.
{
return new Leaf(*this);
}
};
I'm getting the error:
overriding virtual function return type differs and is not covariant from 'Cloneable::clone'
If I change the type to Cloneable *
, I get this error message:
'return' : ambiguous conversions from 'Leaf *' to 'Cloneable *'
My Questions (all related):
- How can the leaf class resolve the
requirements of the
Cloneable
interface? - Is there a better solution to implement a Cloning contract, where all objects are guaranteed to implement cloning?
I'm using this paradigm as part of generic programming (records, fields & database).
Compiler: MS Visual Studio 2008; Platforms: Windows XP & Vista