I’m working on a c++ app and I’m facing a problem:
I have a class B derived from the abstract class A that has some event handling methods. A third class C is derived from B and must reimplement some of B methods. Is there a way to implicitly call B’s method before calling C’s one?
Class diagram:
class A
{
virtual void OnKeyPress(event e)=0;
};
class B : public A
{
virtual void OnKeyPress(event e)
{
print("Keypressed: "+e)
};
};
class C : public B
{
void OnKeyPress(event e)
{
//DoSomething
}
}
One of the workaround I figured out is to call the parent’s method from C using, say, B::foo() inside C::foo(). This works but it is up to the developer to remember to add the call in the method’s body.
The other is to define a new virtual method that the child will override and that the parent will call inside its “OnKeyPress” method.
Thank you,
3mpty.
You have to explicitly call the base class method.
Just re-read your question…..
Best thing to do is to implement method in B that lastly calls an additional protected virtual method to be implemented by C.
i.e.
And you could make the AdditionalKeyStuff(…) in B pure virtual if you want to force any decendants to override it.