ProgrammingModerateTCS NQT · HCL

In C++, a Base pointer holds a Derived object created with new, and the Base destructor is NOT virtual. What happens on delete basePtr?

  1. A. The Derived destructor runs first, then the Base destructor, always
  2. B. Only the Base destructor runs, and this is guaranteed to be safe
  3. C. A compile-time error is reported
  4. D. Undefined behaviour; the Derived destructor is not guaranteed to run

Correct answer: D. Undefined behaviour; the Derived destructor is not guaranteed to run

Deleting a derived object through a base pointer without a virtual destructor is undefined behaviour, commonly causing leaks of the derived part.

More OOP with C++ and Java questions

Exams that ask this topic