C++ constructors vs. destructors: which statement correctly compares their ability to take arguments and return values?

Difficulty: Easy

Correct Answer: Constructors can take arguments but destructors cannot.

Explanation:


Introduction / Context:
Special member functions govern object lifetime in C++. Constructors initialize a newly created object; destructors finalize and clean it up. Understanding the exact grammar constraints for these functions—such as whether they accept parameters, return values, or allow overloading—is essential for writing correct RAII-style code and for interpreting compiler errors accurately.


Given Data / Assumptions:

  • A constructor can be default, parameterized, copy, or move.
  • A destructor has the form ~ClassName() and is invoked automatically at the end of lifetime.
  • We consider standard C++ behavior without extensions.


Concept / Approach:

Constructors may take parameters; that is precisely how you support configurable initialization (e.g., Vector(int n), String(const char*)). Destructors, by contrast, are declared with an empty parameter list and can never take arguments. Neither constructors nor destructors return a value (not even void). Overloading is permitted for constructors (multiple parameter lists), but there is exactly one destructor per class, optionally virtual, and it cannot be overloaded. Therefore, the only correct comparison in the options is that constructors can take arguments but destructors cannot.


Step-by-Step Solution:

Assess A: false — destructors cannot take parameters; constructors can. Assess B: true — matches the language rules. Assess C: false — constructors can be overloaded; destructors cannot. Assess D: false — neither returns any value.


Verification / Alternative check:

Attempt to declare ~C(int) → compile-time error. Define multiple constructors with distinct parameter lists → valid overloading. Attempt to give constructors/destructors a return type → rejected by the compiler.


Why Other Options Are Wrong:

A: flips the truth about parameters.

C: reverses overloading realities.

D: contradicts the rule that special members have no return type.


Common Pitfalls:

  • Confusing destructor parameters with user data passed to cleanup code (instead, store needed state as members).
  • Assuming multiple destructors are possible; use conditional cleanup inside the single destructor.


Final Answer:

Constructors can take arguments but destructors cannot.

Discussion & Comments

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