Difficulty: Medium
Correct Answer: An explicit constructor is one declared with the explicit keyword to prevent the compiler from using it for unintended implicit conversions or copy initialization.
Explanation:
Introduction / Context:
The explicit keyword in C++ is used with constructors to control type conversions. Without explicit, a single argument constructor can be used for implicit conversions, which sometimes leads to subtle bugs. This question tests whether you know what an explicit constructor is and why it is an important tool for designing clear and safe class interfaces.
Given Data / Assumptions:
Concept / Approach:
A constructor declared with the explicit keyword is called an explicit constructor. It can still be used for direct initialization and explicit casting, but the compiler will not use it automatically to convert from the argument type to the class type in contexts that require implicit conversions. This gives you control over what conversions are allowed and avoids unexpected behaviour in function overload resolution or arithmetic expressions involving user defined types.
Step-by-Step Solution:
Step 1: Consider a class with a single parameter constructor: class Meter { double m; public: Meter(double v) : m(v) {} };
Step 2: Without explicit, an expression like Meter d = 5.0; or passing 5.0 where a Meter is expected will work via implicit conversion.
Step 3: If this is undesirable, change the declaration to explicit Meter(double v); which disables such implicit conversions.
Step 4: You can still construct explicitly: Meter d(5.0); or Meter d = Meter(5.0);.
Step 5: Therefore, an explicit constructor is explicitly marked to avoid automatic, possibly unintended conversions.
Verification / Alternative check:
Compile a sample where a function void f(Meter); is defined. With a non explicit constructor, calling f(5.0); compiles because 5.0 is implicitly converted to Meter. After adding explicit to the constructor, the same call fails to compile, and you must write f(Meter(5.0)); instead. This experiment confirms the control that explicit provides over implicit conversions.
Why Other Options Are Wrong:
Option B is wrong because any accessible constructor can be called directly with parentheses, not just explicit ones. Option C incorrectly claims that explicit constructors must be used with new; allocation style is unrelated. Option D confuses explicit constructors with default constructors or constructors that supply default values for members, which is a separate feature.
Common Pitfalls:
A common pitfall is forgetting to mark single argument constructors as explicit, leading to surprising implicit conversions that change overload resolution or arithmetic expressions. Another mistake is overusing explicit in places where implicit conversion is actually convenient and safe. Good design requires balancing explicitness and convenience by carefully deciding which constructors should allow implicit conversions.
Final Answer:
An explicit constructor is one declared with the explicit keyword to prevent the compiler from using it for unintended implicit conversions or copy initialization.
Discussion & Comments