2

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?

2
  • 3
    Check out boost::base_from_member. Commented May 25, 2015 at 12:34
  • 1
    "This has the disadvantage that buildCache_() can't be const." In your second example it should be static. Commented May 25, 2015 at 12:37

2 Answers 2

4

What you are doing as-is is undefined behavior, from [class.base.init]/14, emphasis mine:

Member functions (including virtual member functions, 10.3) can be called for an object under construction. Similarly, an object under construction can be the operand of the typeid operator (5.2.8) or of a dynamic_cast (5.2.7). However, if these operations are performed in a ctor-initializer (or in a function called directly or indirectly from a ctor-initializer) before all the mem-initializers for base classes have completed, the result of the operation is undefined.

What you want to do instead is use the Base-from-Member idiom:

class Cache {
protected:
    const SomeDataType cache_;
};

class B : private Cache, public A {
public:
    B() : Cache(), A(cache_)
    { }
};
Sign up to request clarification or add additional context in comments.

5 Comments

In C++11, you are allowed to call a constructor from within a constructor, correct? In that case, for base-from-member cases such as this, what is the advantage of a separate class for the member as opposed to simply B() { buildCache_(); A(cache_); }?
@R_Kapp That is incorrect. That code would first call A's default constructor, and then within the body default-construct an object of type A named cache_.
Would changing it to this->A(cache_); provide better results?
@R_Kapp No, that's just invalid syntax. It's not possible to do what you're suggesting.
@R_Kapp: your sample is not a forward constructor, see my answer for the correct syntax.
1

From C++11, you may use forward constructor:

class B: public A
{
public:
  B(): B(B::buildCache_()) {}

private:
  B(SomeDataType&& cache) : A(cache), cache_(std::move(cache)) {}

private:
  static SomeDataType buildCache_()
  {
    // expensive cache construction
  }

protected:
    const SomeDataType cache_;
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.