Note: Not a duplicate of C++ 11 Delegated Constructor Pure Virtual Method & Function Calls -- Dangers?. This other question refers to a conceptually similar problem that doesn't really present a solution for this case.
Consider the following program:
#include <iostream>
using std::cout;
using std::endl;
class Base {
virtual void init() = 0; // a hook function
public:
Base(int a, int b) { /* ... */ init(); }
Base(char a, int b) { /* ... */ init(); }
Base(char a, int b, double* c) { /* ... */ init(); }
/* etc. Dozens of constructors */
};
class Derived1 : public Base {
void init() { cout << "In Derived1::init()" << endl; }
public:
using Base::Base;
};
class Derived2 : public Base {
void init() { cout << "In Derived2::init()" << endl; }
public:
using Base::Base;
};
int main() {
Derived1 d1(1, 2);
Derived2 d2('a', 3);
return 0;
}
This code obviously does not run (though it does compile with warnings on some compilers). The question is, what is the best way to implement this sort of pattern? Assuming there are dozens of derived classes and dozens of constructors in the Base, reimplementing the Base constructors in the derived class (with calls to base constructors and init() in the body of the derived constructor) is not really ideal.