can I change the type of the return value when overriding a virtual function in C ++?
Only in very limited mode, in that the (raw) pointer or reference return type can be covariant.
is there a good solution to this problem?
Well, there are two pretty good solutions and one weak solution.
I offer you a little bad solution. One of the reasons I give this is because it is easy to understand or at least quite easy to “copy and modify”, even if it is not entirely clear. Another reason is that one of the good solutions requires some broad general support equipment, which does not have room for discussion here, and another good solution (which, in my opinion, is best in all respects), has the form that at least when I presented such a solution, I automatically received a disk with downvotes and only this, here, on SO. I assume that the price of paying for diversity is here, which variety is a very good thing :-) But, unfortunately, this means that it makes no sense to offer real good things, then I would refuse a negative reputation.
In any case, code based on dominance in virtual inheritance; it is about the same as inheriting an interface implementation in Java or C #:
#include <iostream> #include <string> #include <sstream> //--------------------------------------- Machinery: class ToStringInterface { public: virtual std::string toString() const = 0; }; template< typename ValueProvider > class ToStringImpl : public virtual ToStringInterface { public: virtual std::string toString() const { ValueProvider const& self = *static_cast<ValueProvider const*>( this ); std::ostringstream stream; stream << self.value(); return stream.str(); } }; //--------------------------------------- Usage example: class Object : public virtual ToStringInterface { // ... }; class Long : public Object , public ToStringImpl< Long > { public: typedef long long BasicType; Long( BasicType v ): value_( v ) {} BasicType value() const { return value_; } private: BasicType value_; }; class Int : public Object , public ToStringImpl< Int > { public: typedef int BasicType; Int( BasicType v ): value_( v ) {} BasicType value() const { return value_; } private: BasicType value_; }; int main() { Object const& obj = Int( 42 ); std::cout << obj.toString() << std::endl; }
If your classes are Long and Int , etc. look very much the same as they seem, consider defining only one class template, or perhaps inherit from the specializations of such a template (this can also help avoid errors, as this reduces redundancy).
EDIT . Now I see that you accepted the answer, which is essentially my last suggestion on templates. This means that I answered the question posed (a solution for different, different classes), while you had something less general. Oh good.
Cheers and hth.,
Cheers and hth. - alf
source share