Override a member function with different return type

梦想与她 提交于 2019-11-27 20:33:18

Overriding essentially means that either the Base class method or the Derived class method will be called at run-time depending on the actual object pointed by the pointer.
It implies that:
i.e: Every place where the Base class method can be called can be replaced by call to Derived class method without any change to calling code.

In order to achieve this the only possible way is to restrict the return types of the overriding virtual methods to return the same type as the Base class or a type derived from that(co-variant return types) and the Standard enforces this condition.

If the above condition was not in place it would leave a window to break the existing code by addition of new functionality.

In order to override a virtual function, the return value must be exactly the same*. C++ will not automatically convert between double and int here - after all, how would it know what return type you want when calling from a derived class pointer? Note that if you change part of the signature (parameters, const-ness, etc), then you can change the return value as well.

* - strictly speaking, it must be 'covariant'. What this means is that the type you return must be a subset of the parent function's return type. For example, if the parent class returns a base *, you could return a derived *. Since deriveds are simultaneously also bases, the compiler lets you override in this manner. But you can't return totally unrelated types such as int and double; just because there's an implicit conversion doesn't mean the compiler will let you do this kind of override.

ezod

See this question. To summarize, you can only override a virtual function using a different return type if the types are covariant.

If you want to override, you should try to use template.

See the following:

#include <iostream>

using namespace std;

class base
{
   public:
      template<typename X> X func()
      {
         cout << "vfunc in base class\n";
         return static_cast<X>(0);
      }  
};    

class derived: public base
{
   public:
      template<typename X> X func()
      {
         cout << "vfunc in derived class\n";
         return static_cast<X>(2);
      }  
};    

int main()
{
   derived *bptr = new derived;
   cout << bptr->func<int>() << endl;
   cout << dynamic_cast<base*>(bptr)->func<int>() << endl;

   derived *bptr2 = new derived;
   cout << bptr->func<double>() << endl;
   cout << dynamic_cast<base*>(bptr)->func<int>() << endl;


   return 0;
}

Of course, you dont need to declare it on two different class that way, you could do:

class base
{
   public:
      int func()
      {
         cout << "vfunc in base class\n";
         return 0;
      }  

      double func(){
        cout << "vfunc for double class\n";
        return 2.;

      }
};

Overriding is not posssible ,as the signatures are different. The basic purpose of overriding is polymorphism but it is not possible in the above example

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!