c++ - Force execution of parent's method before child's method without explicit call -
I am working on a C + + app and I am facing a problem: I have a class B, which is obtained from the abstract class, one that has some event handling methods, is obtained from third grade C to B. and it should replicate some of B's methods. There is no way to call B's method before calling a C?
Class diagram:
class A {virtual void OnKeyPress (event e) = 0; }; Class B: Public A {Virtual Zero Oneppress (Event E) {Print ("Keypad:" + E)}; }; Class C: Public B {Zero OKEpress (Event E) {// Doosing}} I have come to know that an alternative solution is to call parent method by using C Do, say, B:: Foo () inside C :: foo () It works, but it depends on the developer that he remembers to add the call to the body of the method. Second, to define a new virtual method on which the child is overridden and parents have their "OKKEP" method.
Thanks, 3 blank.
You must explicitly call the base class method.
Class C: Public B {Virtual Zero OnKeyPress (Event E) {B :: OnKeyPress (E); // to work } };
Read your question again ...
The best part is that implementing the method in B, which ultimately executes an additional secure virtual method To say C
i.e.
class B: public A {protected: virtual zero additional keythof (event e) {} public: virtual zeroes onkeppress (event e) {// Do B Stuff // In the end, liabilities are given an extra kASTST (E)}}; Class C: Public B {Secure: Virtual Zero Extra Kith Stuff (Event E) {// Only C Items}};
And if you want to force any claim to override it then you can make extra key studs (...) in P pure virtual.
Comments
Post a Comment