1. Home
  2. Computer Science MCQs
  3. C++ and Object-Oriented Programming
  4. class D : public A, public B { Mem1 m1; Mem2 m2; public…

Added 30 Nov -0001

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?

  1. m2, m1, B, A for initialisation, matching the initialiser list
  2. m1, m2, A, B for initialisation and A, B, m1, m2 for destruction
  3. A, B, m1, m2 for initialisation and the same order for destruction
  4. A, B, m1, m2 for initialisation and m2, m1, B, A for destruction Correct answer

Correct answer

D. A, B, m1, m2 for initialisation and m2, m1, B, A for destruction

15 views

MCQSEXAM

Explanation

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.

Reference

Asked in: NTS, PPSC IT, Campus Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is D? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More C++ and Object-Oriented Programming MCQs

Practise all C++ and Object-Oriented Programming MCQs

Similar MCQs