You can't overload based on the return type. The simple solution is to not make the copy()
function the virtual
interface but rather have it just call the virtual
interface:
class Base {
Base* do_copy() const = 0;
public:
smart_ptr<Base> copy() const { return smart_ptr<Base>(this->do_copy()); }
};
class Derived {
Derived* do_copy() const { return new Derived(*this); }
public:
smart_ptr<Derived> copy() const { return smart_ptr<Derived>(this->do_copy()); }
};
The idiom of not making virtual functions public is used quite consistently in the standard library (I think the exception is std::exception::what()
). It also conveniently side-steps the issue of an override hiding other overloads (see, e.g., the put()
and do_put()
members of std::num_put<...>
).