I have a class A
and a class B
that inherits from A
. A
has a foo
method which I would like to override in B
.
class A {
public:
void foo();
...
}
class B: public A {
public:
void foo();
...
}
The solution to this would of course be to define A::foo()
as a virtual method, by declaring it as virtual void foo();
. But the problem is that I can't do that, since the A
class is defined in a third-party library so I would rather not change its code.
After some searching, I found the override
keyword (so that the declaration for B::foo
would be void foo() override;
), but that didn't help me since that's not what override
is for, override
can apparently only be used on virtual methods to be sure that the method is really overriding another method and that the programmer didn't make a mistake, and it generates an error if the method isn't virtual.
My question is how can I achieve the same effect as making A::foo
virtual without changing any of the code for A
, but only the code for B
?