2

I would like the compiler to always invoke a base virtual function when any of its overriding functions are called, without having to explicitly write it down in the overriders' definitions. The compiler already does this for virtual destructors, i.e., the derived class's destructor implicitly calls its base class's destructor. Is there some sort of keyword that I can use to enable similar behavior in a non-destructor?

class Base {
public:
  virtual ~Base() {}
  virtual void foo() {}
};

class Derived : public Base {
public:
  ~Derived() {
    // Compiler automatically calls Base::~Base()
  }
  void foo() override {
    // I want Base::foo() to be implicitly called here
  }
};
Dio
  • 35
  • 3
  • Good question! See https://en.wikipedia.org/wiki/Template_method_pattern. The answers in the duplicate are on the weak side in my opinion. – Bathsheba Aug 28 '20 at 20:50

0 Answers0