Performing a basic function before continuing a derived function

I am trying to solve a problem when I have some classes in which I need to do some general work, and then a bunch of problem work, and when this is completed, do another processing common to all these classes.

I have a Base and Derived class that has a function called Execute. When I call a derived version of this function, I would like to be able to do some processing common to all my derived classes in the database, and then continue execution in my Derived :: Execute and return to Base :: Execute to finish with some general work .

Is this possible in C ++ and what is the best way to do this?

This is an idea, but it is probably not very workable:

class Base { public: virtual void Execute(); }; Base::Execute() { // do some pre work Derived::Execute(); //Possible???? // do some more common work... } class Derived : public Base { public: void Execute(); }; void Derived::Execute() { Base::Execute(); //Do some derived specific work... } int main() { Base * b = new Derived(); b.Execute(); //Call derived, to call into base and back into derived then back into base } 
+4
source share
4 answers

Use a pure virtual function from the database.

 class Base { public: void Execute(); private: virtual void _exec() = 0; }; Base::Execute() { // do some common pre work // do derived specific work _exec(); // do some more common work... } class Derived : public Base { private: void _exec() { // do stuff } }; int main() { Base * b = new Derived(); b.Execute(); } 

EDIT: changed the thread a bit after reading the question yet ... :) The above mechanism should match exactly what you need now -

i.e.

  • Basic general material
  • Derived Concrete Things
  • Basic general stuff again
+9
source

This is called an NVI (not a virtual interface, from Herb Sutter here ) idiom in C ++, and basically says that you should not have public virtual functions, but rather protected / private virtual functions. Custom code will have to call your public non-virtual function in the base class, and this will send the protected / private virtual method.

From a design point of view, the rationale is that the base class has two different interfaces, on the one hand, a user interface defined by a public subset of the class, and on the other hand, an extensibility interface and a way to extend the class. Using NVI, you decouple both interfaces and provide more control in the base class.

 class base { virtual void _foo(); // interface to extensions public: void foo() { // interface to users // do some ops _foo(); } }; 
+4
source

Turn the problem from head to foot. What you really want to have is a base class algorithm that derived classes can connect to:

 class Base { public: void Execute() { // do something execute(); // do some more things } private: virtual void execute() = 0; }; class Derived : public Base { public: // whatever private: virtual void execute() { //do some fancy stuff } }; 

Providing labels for derived classes in base class algorithms is often referred to as the template template template (which has nothing to do with template ). Non-public virtual functions in the base class interface are often called a non-virtual interface .

I'm sure Google can find you a lot on these two.

+3
source

Move this Base::Execute internally into two functions, and then use RAII to implement this easily.

 class Base{ protected: void PreExecute(){ // stuff before Derived::Execute } void PostExecute(){ // stuff after Derived::Execute } public: virtual void Execute() = 0; }; struct ScopedBaseExecute{ typedef void(Base::*base_func)(); ScopedBaseExecute(Base* p) : ptr_(p) { ptr_->PreExecute() } ~ScopedBaseExecute() { ptr_->PostExecute(); } Base* ptr_; }; class Derived : public Base{ public: void Execute{ ScopedBaseExecute exec(this); // do whatever you want... } }; 
+1
source

Source: https://habr.com/ru/post/1348056/


All Articles