Skip to content

Order of construction with a base and a member

Question

Class B publicly inherits from class A and contains a data member of class C. When a B object is created and later destroyed, in what order do the constructors and destructors of A, B and C execute?

Short answer

Construct AA, then CC, then BB; destroy BB, then CC, then AA. Base classes are built first, then members in declaration order, then the derived constructor body runs. Destruction is the exact reverse.

Adapted from Joshi, Denson & Downes, Quant Job Interview Questions and Answers, Q7.13, p.226; question and answer rewritten by SelfMadeQuant.com.

See the hint and the full worked solution in the question bank (free account).

Open the worked solution

Lessons behind this question