class D : public A, public B { Mem1 m1; Mem2 m2; public: D() : m2(2), m1(1), B(), A() {} }; In what order are bases and members initialised, and destroyed?
Correct answer
D. A, B, m1, m2 for initialisation and m2, m1, B, A for destruction
15 views
D. A, B, m1, m2 for initialisation and m2, m1, B, A for destruction
15 views
Initialisation order follows declaration: base classes in the order of the base-clause, then non-static members in declaration order, then the constructor body, regardless of how the mem-initialiser list is written. Destruction is the exact reverse, so m2 dies first and A last. Compilers warn with -Wreorder when the list disagrees, because depending on list order causes real uninitialised reads.
Asked in: NTS, PPSC IT, Campus Interviews
Not sure why the answer is D? Ask here and get an explanation.