Which statement is correct about C++ destructors and their return type? Choose the precise language rule.

Difficulty: Easy

Correct Answer: A destructor has no return type.

Explanation:


Introduction / Context:
Destructors finalize an object's lifetime and release resources. Their syntax is constrained to ensure predictable cleanup. This question focuses on what a destructor may return.



Given Data / Assumptions:

  • Destructor syntax is ~ClassName().
  • No parameters and no overloading by signature are allowed.
  • Destructors cannot be const, static, or templated in the usual sense.



Concept / Approach:
By the C++ standard, a destructor has no return type and cannot return a value. Writing any return type (e.g., void) is ill-formed because the grammar fixes destructor declarations to the ~ClassName() form only. Control may leave the destructor normally or by throwing an exception (which is strongly discouraged), but there is never a return value.



Step-by-Step Solution:
1) Consider a valid form: struct T { ~T(); };2) Attempt to write void ~T(); → compilation error: return type not permitted.3) Likewise, int ~T(); is invalid for the same reason.4) Therefore the only correct statement is that a destructor has no return type.



Verification / Alternative check:
Compile test snippets with various faux return types on destructors; compilers reject them according to the grammar.



Why Other Options Are Wrong:
Void/integer/same as main/boolean: all contradict the fixed grammar for destructors.



Common Pitfalls:
Attempting to signal cleanup errors via a destructor return value. Use exception-safe design (prefer noexcept destructors) and RAII so failure paths are explicit outside destructors.



Final Answer:
A destructor has no return type.


Discussion & Comments

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