Difficulty: Easy
Correct Answer: call itself recursively
Explanation:
Introduction / Context:
The copy constructor is meant to copy an existing object into a new one. Its parameter type is critical. A common trap is to declare the copy constructor with a by-value parameter instead of a by-reference parameter. This question explores the consequence of that mistake.
Given Data / Assumptions:
Concept / Approach:
Passing an object by value to a function requires copying it to create the parameter. If the function in question is the copy constructor itself, creating the parameter requires invoking… the copy constructor. That, in turn, requires copying the parameter again, and so on—leading to infinite recursion at compile time (conceptually), which manifests as the inability to form the parameter without another copy. Hence, the correct design is to take the source by reference, typically const T&
(and optionally also define a move constructor in modern C++).
Step-by-Step Solution:
Verification / Alternative check:
Change the signature to T(const T&)
. Recompile and observe that the issue disappears. Optionally implement T(T&&)
for move semantics. Tools like static analyzers flag the by-value copy constructor as erroneous.
Why Other Options Are Wrong:
One-argument or zero-argument constructors are irrelevant here.
“Work without any problem” ignores the recursive dependency inherent in by-value parameters.
Common Pitfalls:
const
on the reference, reducing usability with const objects.
Final Answer:
call itself recursively
Discussion & Comments