Why no compiler error when virtual function has different parameter type ?
-
Hi All,
I know the fact that different parameter type in virtual function is not qualified for override, but how compiler was able to detect error for return type's type mismatch, whereas it's not able to find error with parameter type ?Thanks!
-
@Vinoth-Rajendran4 said in Why no compiler error when virtual function has different parameter type ?:
not able to find error with parameter type ?
The compiler can't tell if you want to override or overload.
C++11 has a new feature to detect these errors: You should always add the "
override
" keyword when you want to override a virtual function.class Base { public: virtual void func(int x); }; class DerivedA : public Base { public: virtual void func(float x); // No error: This is a new overload with `float` arg }; class DerivedB : public Base { public: void func(float x) override; // ERROR: This is not a valid override }; class DerivedC : public Base { public: void func(int x) override; // No error: This is a valid override };
-
@JKSH : Thanks for quick reply.
With virtual keyword present in function, its obvious that we are expecting function override. Then why compiler fails to detect the error without we using "override" keyword ?
-
@Vinoth-Rajendran4 said in Why no compiler error when virtual function has different parameter type ?:
With virtual keyword present in function, its obvious that we are expecting function override.
No, it is not obvious -- it is ambiguous. "
virtual
" can mean "override an existing function" or "create a new virtual function"."
override
" makes it obvious that we are expecting a function override. Just use it, and you don't have to worry about this issue anymore.why compiler fails to detect the error without we using "override" keyword ?
Because it is a logical error, not a compilation error. Compilers don't detect logical errors.
Let me ask you: Why does the compiler fail to detect the following error?:
int *x = nullptr; *x = 42;
-
int *x = nullptr; *x = 42;
This is logical error. compiler can't find the issue here.
But I got curious to ask this question in forum because,
class Base { public: virtual void fun() { cout << "Base \n"; } }; class Derived : public Base { public: bool fun() // compiler error in this case , return type different { cout << "Derived \n"; } };
whereas no error here,
class Base { public: virtual void fun(int) { cout << "Base \n"; } }; class Derived : public Base { public: void fun(bool) // no error, parameter type different { cout << "Derived \n"; } };
Can you help me understand ,how compiler is able to detect return type mismatch in case virtual keyword is used ?
-
Because you can't overload a function when the return type is different.
-
@Vinoth-Rajendran4 You get an error with different return type because the signature of the virtual method is same, so compiler considers it to be the same method (return type is not part of the signature). Compiler simply could not decide reliably which version of a virtual method to call if the only difference would be its return type.
But if you add a virtual method with same name but different parameter list then you are overloading that method, so why should compiler complain? It is perfectly fine to overload virtual methods like any other method. -
@Vinoth-Rajendran4 said in Why no compiler error when virtual function has different parameter type ?:
Can you help me understand ,how compiler is able to detect return type mismatch in case virtual keyword is used ?
@Christian-Ehrlicher and @jsulm have the correct answer.
This also applies to non-virtual functions: You cannot have 2 functions with the same name and same parameters but different return types.
// OK: Overloaded functions can have different return types void funA(); bool funA(int); // Error: Not allowed to have same name and same parameters but different return types void funB(); bool funB();
-
@JKSH said in Why no compiler error when virtual function has different parameter type ?:
You cannot have 2 functions with the same name and same parameters but different return types
You can in two different classes in same class hierarchy as long as the method is not virtual. But in this case you're overriding anyway.
-
@jsulm said in Why no compiler error when virtual function has different parameter type ?:
@JKSH said in Why no compiler error when virtual function has different parameter type ?:
You cannot have 2 functions with the same name and same parameters but different return types
You can in two different classes in same class hierarchy as long as the method is not virtual.
Ooh, good point
-
I got the point.
Virtual functions in inheritance hierarchy, stays virtual as long as the signature is same( return type not part of signature). But once signature varies, overloading comes into picture, and overloading rules apply for those functions.