I'm struggling a bit with the following situation: Lets say I have a class "Base" which requires some information in order to work properly. Therefore, I've set up a single constructor with the appropriate parameters, e.g.:
class Base {
public: Base(X x, Y y, Z z);
};
This works fine for general "bases". But I'd like to have subclasses that represent specific "bases" which are often used and which contain all information required to setup the base class, e.g.:
class Derived {
public: Derived() {
X x;
x.addSomeInformation("foo bar");
// ...
// now what?
}
};
Problem here is that I cannot invoke the parent constructor via the initializer list (as one would usually do) becaues the parameters to be handed over do not yet exist.
Due to this, I feel that something is seriously wrong with my design. I can make this work by introducing a protected default constructor and a "configure" method that imitates the normal constructor, but I'm not sure if this is a good idea (mainly because this way I cannot ensure that the subclass actually invokes "configure" - and if it doesn't that would leave me with a not properly initialized base class):
class Base {
public: Base(X x, Y y, Z z) { configure(x, y, z); }
protected: Base() {}
protected: void configure(X x, Y y, Z z);
};
class Derived {
public: Derived() {
X x;
// ...
configure(x, y, z);
}
};
How would one deal with such a situation in a proper way?