I have a question about the way classes using inheritance are constructed. In the example below, the call to the Base
constructor is dependent on a function implemented in the Derived
class. This function is again dependent on the Derived
member generator
, which will not be initialized until after the Base
constructor call.
If the Base
class is constructed first, won't the variable Base::in_
contain garbage data?
class Derived
: public Base
{
Derived()
: Base(get_data()),
generator(5) {}
Generator generator;
int get_data() { return generator.get_some_data(); }
};
class Base
{
Base(int in)
: in_(in) {}
int in_;
}