Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override a member function with different return type

Consider the example below:

#include <iostream>  using namespace std;  class base {    public:       virtual int func()       {          cout << "vfunc in base class\n";          return 0;       } };  class derived: public base {    public:       double func()       {          cout << "vfunc in derived class\n";          return 0;       } };  int main() {    base *bptr = new derived;    bptr->func();     return 0; } 

The compiler gives an error for the above code that there is conflicting type for the overriden function. Why is it not possible to override a function in the derived class with a different return type ?

I believe, in-order to override a function, the base class virtual method needs to be redefined in the derived class. To redefine a method, the signatures of the methods has to be the same. Since return type is not part of the signature, i believe even if there is difference in return type, the method will still be redefined? In that case for the code above, virtual function func is redefined in the derived class with a different return type. But the compiler throws an error. Is my understanding correct?

like image 895
nitin_cherian Avatar asked Jan 23 '12 04:01

nitin_cherian


People also ask

Can I override with different return type?

Yes. It is possible for overridden methods to have different return type . But the limitations are that the overridden method must have a return type that is more specific type of the return type of the actual method.

Can we override method with different return type C++?

To summarize, you can only override a virtual function using a different return type if the types are covariant. The types aren't covariant, the override is. No, only your interpretation of it is a bit idiosyncratic.

How do you override a member function?

Access Overridden Function in C++ To access the overridden function of the base class, we use the scope resolution operator :: . We can also access the overridden function by using a pointer of the base class to point to an object of the derived class and then calling the function from that pointer.

Can we change return type in method overriding in C#?

You can't change the return type of a method by overriding it.


2 Answers

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.

like image 132
Alok Save Avatar answered Sep 23 '22 17:09

Alok Save


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.

like image 45
bdonlan Avatar answered Sep 19 '22 17:09

bdonlan