I have the following code:
struct Abs {
virtual void f(int x) = 0;
virtual void f(double x) final { std::cout << 2; }
};
struct Sub: public Abs {
void f(int x) final { std::cout << 1; }
};
Abs
is an abstract class which comprises a pure member function void f(int)
and its overloaded version void f(double x)
, which is no longer pure and final
. If I try to override void f(int)
in the derived struct Sub
, it shadows void f(double)
and the following main
function prints 1
, converting 1.01
to int
:
int main() {
Sub x = {};
x.f(1.01);
return 0;
}
How do I overcome this problem? Also, why does it work like that?
Sub
to have a lineusing Abs::f
, which has the effect of making both versions ofAbs::f()
candidates when calling when callingf()
within context ofSub
.