Consider the following example:
#include <iostream>
#include <string>
class Base {
public:
virtual void func(int a) {}
};
class Derived : public Base {
public:
void func( const int a) override {
}
};
int main()
{
Derived d;
d.func(1);
return 1;
}
I override the func
method but add const to the parameter, in which case the linker should scream that something is wrong. Either the function is not overridden or the function parameter should not be const.
But to my surprise, this code links and works.
You can find an online example here.
Am I missing something? Why does this code work?
Although similar to Functions with const arguments and Overloading it addresses a different problem. That question was about not being possible to overload a method of the base class, while this question addresses the problem of being able to override a derived method.