In C++, a subclass can specify a different return type when overriding a virtual function, as long as the return type is a subclass of the original return type (And both are returned as pointers/references).
Is it possible to expand this feature to smart pointers as well? (Assuming a smart pointer is some template class)
To illustrate:
class retBase {...};
class retSub : public retBase {...};
class Base
{
virtual retBase *f();
};
class Sub : public Base
{
virtual retSub *f(); // This is ok.
};
class smartBase
{
virtual smartPtr<retBase> f();
};
class smartSub : public smartBase
{
virtual smartPtr<retSub> f(); // Can this be somehow acheived?
};
EDIT: As Konrad Rudolph suggested, this is not directly possible. However, I ran accross this method:
class smartBase
{
protected:
virtual retBase *f_impl();
public:
smartPtr<refBase> f()
{
return f_impl();
}
};
class smartSub : public smartBase
{
protected:
virtual retSub *f_impl();
public:
smartPtr<refSub> f()
{
return f_impl();
}
};
Would you suggest going this way?