it is possible to change return type when override a virtual function in C++?

后端 未结 6 1396
醉梦人生
醉梦人生 2020-12-16 03:34

I encounter a problems about override virtual functions, in fact,it is about hessian (a web service protocol).

it has a base class Object, and some derived classes :

6条回答
  •  -上瘾入骨i
    2020-12-16 04:07

    I don't think you're going about this the right way. While it is possible in some circumstances to change the return type of a virtual function, consider this: How is your function being used? If it's virtual, changes are that users will be using the base class. As such, they are oblivious as to what the actual type of your class is, and thus they wouldn't know what type to expect. So:

    • Either return the base class type.
    • Return functions that give you the proper type (i.e virtual std::string getStringValue(), which gives you a string if applicable).
    • Use templates, if the type is known by the user.

提交回复
热议问题