M4rc M4rc - 8 days ago 6
C++ Question

Automatic calling of base class functions

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?

Answer

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);
}