Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing base function before continuing in derived function

I'm trying to solve a problem where I have some classes in which I need to do some common work and then a bunch of problem specific work and when this is finished do some more processing common to all these classes.

I have a Base and Derived class that both have a function called Execute. When I call the derived version of this function, I'd like to be able to do some processing common to all my derived classes in the Base and then continue executing in my Derived::Execute and going back to Base::Execute to finish off with some common work.

Is this possible in C++ and how would one best go about doing that?

This is the idea, however it's probably not very workable like this:

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

}
like image 388
Tony The Lion Avatar asked Dec 03 '22 08:12

Tony The Lion


2 Answers

Use a pure virtual function from base..

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 flow slightly after reading the question some more.. :) The above mechanism should match exactly what you require now -

i.e.

  1. Base Common Stuff
  2. Derived specific stuff
  3. Base Common stuff again
like image 130
Nim Avatar answered Dec 21 '22 22:12

Nim


This is called the NVI (Non-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. User code will have to call your public non-virtual function in the base class, and that will dispatch through to the protected/private virtual method.

From a design perspective the rationale is that a base class has two different interfaces, on one side the user interface, determined by the public subset of the class, and on the other end the extensibility interface or how the class can be extended. By using NVI you are decoupling both interfaces and allowing greater control in the base class.

class base {
   virtual void _foo();  // interface to extensions
public:
   void foo() {          // interface to users
      // do some ops
      _foo();
   }
};
like image 22
David Rodríguez - dribeas Avatar answered Dec 21 '22 22:12

David Rodríguez - dribeas