I would like to initialize a class B that's derived from class A, and where in B I construct a cache first that is used with the construction of A, e.g.,
class B: public A {
public:
B(): A(cache_), cache_(this->buildCache_())
protected:
const SomeDataType cache_;
private:
SomeDataType
buildCache_() const
{
// expensive cache construction
}
}
This is not going to work though because the parent object A is always initialized first (before cache_ is filled).
(For the sake of completeness: The cache_ is used many more times in classes derived from B.)
As an alternative, I could do
class B: public A {
public:
B(): A(this->buildCache_())
protected:
const SomeDataType cache_;
private:
SomeDataType
buildCache_()
{
// expensive cache construction
// fill this->cache_ explicitly
return cache_;
}
}
This has the disadvantage that buildCache_() can't be const. Also, GCC complains that
warning: ‘B::cache_’ should be initialized in the member initialization list [-Weffc++]
Is there a more appropriate solution to this?
boost::base_from_member.buildCache_()can't beconst." In your second example it should bestatic.