C++ destructors: which statement correctly describes what a destructor destroys?

Difficulty: Easy

Correct Answer: Destructor destroys the complete object.

Explanation:


Introduction / Context:
Destructors finalize objects by releasing resources and performing class-specific cleanup. A common misconception is that a destructor is tied to particular member types. In reality, destruction is holistic and ordered, covering all subobjects of the instance. This question asks which statement captures that behavior correctly.


Given Data / Assumptions:

  • A “complete object” includes its base-class subobjects and data members.
  • Member destruction order is reverse of construction order.
  • Pointers are just values; deleting dynamically allocated memory is a separate operation you may perform in the destructor body.


Concept / Approach:

When a class’s destructor runs, the language guarantees that first the destructor body executes (your code), then class members are destroyed automatically in reverse order of their declaration, and finally base classes are destroyed last (again, reverse order relative to construction). Therefore, the entire object is torn down, not only specific member categories. If a class owns dynamic resources via pointers, the destructor should release them, but this is your policy code — not an automatic “pointer-only” rule.


Step-by-Step Solution:

Identify that destruction covers all subobjects: bases and members. Recall order: member fields first (reverse declaration), then base subobjects (reverse inheritance chain). Understand pointer members: unless smart pointers are used, you must explicitly free dynamic memory inside the destructor. Conclude the correct statement is “destroys the complete object.”


Verification / Alternative check:

Instrument constructors and destructors of members and bases; observe paired creation/destruction logging that spans the full object graph, not just certain types like int or float.


Why Other Options Are Wrong:

“Only integer/float/pointer members” — destruction is not selective by type; all subobjects participate.


Common Pitfalls:

  • Forgetting to free heap allocations held by raw pointers, causing leaks.
  • Misunderstanding that smart pointers automatically manage the lifetime of owned resources during destruction.


Final Answer:

Destructor destroys the complete object.

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion