Question 3
Back to Exercise
What is the output of the following C++ program on a 64-bit system?
#include <iostream>
#include <memory>
struct Base {
virtual ~Base() = 0;
};
Base::~Base() { std::cout << "B"; }
struct Mid : Base { ~Mid() override { std::cout << "M"; } };
struct Leaf : Mid { ~Leaf() override { std::cout << "L"; } };
int main() {
std::unique_ptr<Base> p = std::make_unique<Leaf>();
p.reset();
std::cout << '\n';
return 0;
}
- A LMB Correct
- B BML
- C B
- D L
Correct Answer: Option A
The pure virtual destructor makes Base abstract but still runs last – which is why it must be defined.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!