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):
Cloneable
interface?I'm using this paradigm as part of generic programming (records, fields & database).
Compiler: MS Visual Studio 2008; Platforms: Windows XP & Vista
Having your clone
function return a Cloneable *
is correct.
You will get an ambiguous conversion if one of your interfaces also derives from Cloneable
.
Edit: Alf points out in the comments that not only is it possible for Leaf::clone
to return a Leaf*
, it's actually preferable for it to do so. I stand corrected.