C++ default constructors: how many default constructors can a single class have?

Difficulty: Easy

Correct Answer: Only one

Explanation:


Introduction / Context:
Although constructors may be overloaded, the notion of a “default constructor” is specific: it is a constructor callable with no arguments. This question explores how many such default constructors a class can define according to C++ rules and overload resolution constraints.


Given Data / Assumptions:

  • Multiple constructors may exist in a class.
  • A default constructor is any constructor invocable with zero arguments.
  • Signatures that can be called with no arguments are considered equivalent for default construction purposes.


Concept / Approach:

A class can have only one default constructor. You may have many constructors, but at most one of them can be viable with zero arguments. Having more than one would either be the same signature (redefinition error) or lead to ambiguity when invoked with no arguments. You can implement a single default constructor directly or by using default arguments to make one of the parameterized constructors callable with no arguments, but still there is only one effective default constructor in the set.


Step-by-Step Solution:

Define several constructors with distinct parameter lists. Ensure that no more than one can be called with zero arguments. Observe that attempting two zero-argument constructors is an ODR violation (redefinition). Conclude: a class can have only one default constructor.


Verification / Alternative check:

Try compiling a class with two constructors both invocable as T(). The compiler emits an error (duplicate definition or ambiguous call). Refactor so that exactly one is default-constructible, and the code compiles and runs.


Why Other Options Are Wrong:

Two/Three/Unlimited: contradict signature uniqueness and overload resolution rules.


Common Pitfalls:

  • Accidentally producing ambiguity by giving all parameters default values in more than one constructor.
  • Assuming the compiler will choose one arbitrarily—ambiguity is a compile-time error.


Final Answer:

Only one

Discussion & Comments

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