Automatic calling of base class functions


Keywords:c++ 


Question: 

I'm trying to wrap my head around some more advanced points of C++ classes as well as possible (or at least with GNU) compiler/built in functions. All of which based on a logger concept for a (large and extensive) work project . Which brings me to this psuedo-code:

class Base {
     public:
           void increment(){
               ++m_Val;
           }
     private:
           static int m_Val;
};
class Above : private Base{
     public:
           void doA(){
                //Foo
           }
           void doB(){
               //Bar
           }
};

That having been shown as a concept; is there a way so that the mere inheritance of base allows doA() and doB() to implicitly call the base function increment() without me actually adding the function call to increment() in every single function of every single class that inherits it? If not a direct answer, is there a particular term for what I want to do is called? Or is it offered as a part of Boost or other libraries?


3 Answers: 

One way to achieve similar effect is provide a method for overriding and expose a different public method that calling preconditions and postconditions.

class Base {
     public:
           void increment(){
               ++m_Val;
               std::cout << m_Val << '\n';
           }
           void doA(){
                increment();
                implA();
           }
           void doB(){
               increment();
               implB();
           }
    protected:
        virtual void implA() = 0;
        virtual void implB() = 0;

     private:
           int m_Val;
};
class Above : private Base{
    public:
    using Base::doA;
    using Base::doB;
     protected:
        virtual void implA() override {
            std::cout << "a\n";
        }
        virtual void implB() override {
            std::cout << "b\n";
        }
};

int main()
{
    Above a;
    a.doA();
    a.doB();
}

Demo



You'll have to explicitly define when you want your base method to be called, but it can be done by abstracting away the call to increment - try something like this pattern (pseudo-code, using Java terminology as I'm not as immediately familiar with the equivalent C++ syntax but you should get the gist of it):

abstract class Base {
     public:
           void increment(){
               ++m_Val;
           }
           void doA(){
                increment();
                doAInternal();
           }
           void doB(){
               increment();
               doBInternal();
           }
     protected: 
           abstract void doBInternal();
           abstract void doAInternal();
     private:
           static int m_Val;
};
class Above : private Base{
     public:
           void doAInternal(){
                //Foo
           }
           void doBInternal(){
               //Bar
           }
};


After stumbling on to this by pure accident; the solution that most closely resembles what I'm looking for is called "Execute-Around pointer" as defined Here. Particularly the section relating to:

Often times it is necessary to execute a functionality before and after every member function call of a class.

Which is represented on the following code snippet taken from aformentioned link.

class VisualizableVector {
  public:
    class proxy {
      public:
        proxy (vector<int> *v) : vect (v) {
          std::cout << "Before size is: " << vect->size ();
        }
        vector<int> * operator -> () {
          return vect;
        }
        ~proxy () {
          std::cout << "After size is: " << vect->size ();
        }
      private:
        vector <int> * vect;
    };
    VisualizableVector (vector<int> *v) : vect(v) {}    
    proxy operator -> () {
       return proxy (vect);
    }
  private:
    vector <int> * vect;
};
int main()
{
  VisualizableVector vecc (new vector<int>);
  //...
  vecc->push_back (10); // Note use of -> operator instead of . operator
  vecc->push_back (20);
}