自动调用基本 class 函数

Automatic calling of base class functions

我正在尝试围绕 C++ classes 的一些更高级的点以及尽可能(或至少使用 GNU)compiler/built 函数。所有这些都基于(大型和广泛的)工作项目的记录器概念。这让我想到了这个伪代码:

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

这已被展示为一个概念;有没有办法让 base 的单纯继承允许 doA()doB() 隐式调用基函数 increment() 而无需我在每个函数中实际添加对 increment() 的函数调用每个继承它的 class 的功能?如果不是直接的答案,是否有一个特定的术语来描述我想做的事情?还是作为 Boost 或其他库的一部分提供?

实现类似效果的一种方法是提供一种方法来覆盖和公开调用前置条件和后置条件的不同public方法。

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

您必须显式定义何时调用您的基本方法,但这可以通过抽象调用增量来完成 - 尝试类似这种模式(伪代码,使用 Java 术语,因为我不是很熟悉等效的 C++ 语法,但您应该了解它的要点):

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

纯属偶然偶然发现;与我正在寻找的解决方案最相似的解决方案称为 "Execute-Around pointer",定义为 Here。特别是与以下相关的部分:

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

这表示在以下摘自上述 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);
}