For many classes C1
, C2
, and so forth, the initialization looks equal, and it's just a little something differs from class to class. Hence I created a base class B
that hosts the initalizations, e.g.:
class B {
public:
B()
{
// complex initializations
int a = doSomething();
// more complex stuff with `a`
};
virtual int doSomething()
{return 2 * doSomethingHelper();}
protected:
virtual int doSomethingHelper() = 0;
};
class C: public B {
protected:
virtual int doSomethingHelper()
{return 1;}
};
int main() {
C c;
return 0;
}
This code fails with
pure virtual method called
terminate called without an active exception
Aborted (core dumped)
since doSomethingHelper()
is used to initialize B
.
I'm wondering if there is a better design. My objectives are:
Make
C
's user interface as easy as possible:C
has no constructor arguments.Make
C
itself as minimalistic as possible such that other concretizations ofB
are short. Ideally, it only contains thedoSomethingHelper
.
A suggestion for a more sane design would be appreciated.