Override function with various return types

Effect of return type on function override? (As far as I know, return typde is not part of the function / method signature) In the base class, I have a function that takes no arguments, returns int and is pure virtual. In each derived class, I define enum for the return type. The function is redefined in derived classes, that is, it has the same signature, but differs in behavior. The question arises: is not a function override function legal for overriding and return type?

Code example:

 class Base { public: typedef int ret; virtual ret method() = 0; }; class Der1 { public: enum ret1{ ret1_0, ret1_1 }; ret1 method() { return ret1_1;} }; class Der1 { public: enum ret2{ ret2_0, ret2_1 }; ret1 method() { return ret2_0;} }; 
+5
c ++ override return-type
source share
4 answers

You can redefine functions with different types of returned data, but only types of covariance returns are allowed.

The override function means that either the base class method or the Derived class method will be called at run time, depending on the actual object that the pointer points to.
It means that:
ie: Every place where a base class method can be called can be replaced by a call to the Derived method of the class without changing the call code.

To achieve this, the only possible way is to restrict the return types of overriding virtual methods to return the same type as the base class, or the type derived from it (return types of sharing options), and therefore the standard provides this state.

Without this condition, existing code will break by adding new functionality (new override functions).

+4
source share

Short answer: No, this is not allowed or it is better not to put it, but to redefine it, i.e. you do not override Base::method() , but create a new method with the same name. Most compilers will warn you about this. With your sample code, but considering Base::method not purely virtual, consider this:

 void callMethod(Base const& b) { auto a1 = b.method(); //what should the type of a1 be? -> it int. Every time. std::cout << a1 << '\n'; } int main() { Der1 d1; auto a2 = d1.method(); //a2 is ret1_1 of type ret1 callMethod(d1); //calls Base::method and prints that int, not Der1::method } 

You are correct that return types are not part of the function signature. But when redefining virtual functions, a signature is not that important. Β§10.3,7 explicitly indicates:

The return type of an override function must be either identical to the return type of an overridden function or covariant with function classes. If the function D::f overrides the function B::f , the return types of the functions are covariant if they satisfy the following criteria:

- both are class pointers, both are lvalue class references, or both are rvalue class references

- the class of the return type B::f is the same class as the class in the return type D::f , or is the unambiguous and accessible direct or indirect base class of the class in the inverse type D::f

- both pointers or references have the same cv-qualification and the class type in the return type D::f has the same cv qualification as or less cv qualification than the class type in the return type B::f .

+4
source share

what you have is not redefined.

C ++ supports covariant return types for raw pointers and source links.

but that’s it.

+1
source share

We should not change the return type of the function in the base class by overriding it. Changing the return type by hiding the base element to others is not recommended, because it leads to something strange that cannot be used in a polymorphic way.

+1
source share

All Articles