using namespace boost;
class A {};
class B : public A {};
class X {
virtual shared_ptr<A> foo();
};
class Y : public X {
virtual shared_ptr<B> foo();
};
The return types aren’t covariant (nor are they, therefore, legal), but they would be if I was using raw pointers instead. What’s the commonly accepted idiom to work around this, if there is one?
I think that a solution is fundamentally impossible because covariance depends on pointer arithmetic which is incompatible with smart pointers.
When
Y::fooreturnsshared_ptr<B>to a dynamic caller, it must be cast toshared_ptr<A>before use. In your case, aB*can (probably) simply be reinterpreted as anA*, but for multiple inheritance, you would need some magic to tell C++ aboutstatic_cast<A*>(shared_ptr<B>::get()).