I have code like this:
class RetInterface {...}
class Ret1: public RetInterface {...}
class AInterface
{
public:
virtual boost::shared_ptr<RetInterface> get_r() const = 0;
...
};
class A1: public AInterface
{
public:
boost::shared_ptr<Ret1> get_r() const {...}
...
};
This code does not compile. In visual studio it raises "C2555: overriding virtual function return type differs and is not covariant". If I do not use boost::shared_ptr
but return raw pointers, the code compiles (I understand this is due to covariant return types in C++). I can see the problem is because boost::shared_ptr
of Ret1
is not derived from boost::shared_ptr
of RetInterface
. But I want to return boost::shared_ptr
of Ret1
for use in other classes, else I must cast the returned value after the return.
- Am I doing something wrong?
- If not, why is the language like this - it should be extensible to handle conversion between smart pointers in this scenario? Is there a desirable workaround?