C++ constructors: which statement about return types and contents of constructors is correct?

Difficulty: Easy

Correct Answer: A constructor has no return type.

Explanation:


Introduction / Context:
Constructors are special member functions that initialize objects. Their syntax and type system rules differ from ordinary functions. This question checks whether you know the correct rule for a constructor’s return type and dispels common myths about what can appear inside a constructor’s body.


Given Data / Assumptions:

  • C++ constructors are named after the class.
  • They participate in overload resolution during initialization.
  • They may contain arbitrary statements, including function calls.


Concept / Approach:

A constructor has no return type—not even void. Attempting to specify a return type makes it an ordinary function, not a constructor, and compilation fails if you intend it to be a constructor. Inside the constructor body, you can certainly call other functions, initialize members in the member initializer list, and perform checks or throw exceptions if needed. Therefore, the correct statement is that a constructor has no return type.


Step-by-Step Solution:

Recall the form: ClassName(parameters) : member_inits { body } Note the absence of any return type before the name. Understand that function calls are allowed inside the body. Select “A constructor has no return type.”


Verification / Alternative check:

Try compiling int ClassName(); as a “constructor.” The compiler treats it as a normal function declaration, not a constructor, proving that constructors cannot declare a return type. Conversely, placing function calls in the body compiles and runs normally.


Why Other Options Are Wrong:

“Has a return type” or “has a void return type” contradict the language rule.

“Cannot contain a function call” is false; constructors may call functions freely (and often do).


Common Pitfalls:

  • Confusing constructors with factory functions that return constructed objects.
  • Performing heavy logic in constructors that could throw; consider noexcept guarantees or two-phase initialization where appropriate.


Final Answer:

A constructor has no return type.

Discussion & Comments

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