Questions
Practice prompts and answer frameworks
Use these as preparation prompts. The goal is to explain your reasoning, trade-offs and failure cases clearly.
Why should a base class destructor be virtual?
Explain what can go wrong when deleting a derived object through a base-class pointer whose destructor is not virtual. Show how you would fix the design.
Answer framework
- Explain static type vs dynamic type and why destructor dispatch matters.
- Mention undefined behavior or incomplete destruction risk when deleting through a non-virtual base destructor.
- Show a base class with a virtual destructor when it is intended for polymorphic deletion.
- Add design nuance: not every base class needs virtual functions; make non-polymorphic bases protected/non-virtual or final when appropriate.
Can a constructor call a virtual function safely?
A class calls a virtual method from its constructor. Explain what function actually runs and why this is usually a design smell.
Answer framework
- Explain that during base construction, the derived part is not constructed yet.
- Virtual dispatch from constructors/destructors does not behave like normal runtime polymorphism.
- Discuss invariants and partially constructed object state.
- Suggest alternatives: factory function, non-virtual initialize step, composition or dependency injection.
When do you implement the Rule of Five?
Design a resource-owning class. Which special member functions do you define or delete, and when would you prefer the Rule of Zero?
Answer framework
- Start from ownership: what resource is owned, who releases it, and whether copying is meaningful.
- Cover destructor, copy constructor, copy assignment, move constructor and move assignment.
- Prefer Rule of Zero using standard library types when possible.
- Explain noexcept moves and strong exception safety for assignment.