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 .
Arne mertz
source share