Difficulty: Easy
Correct Answer: copy constructor
Explanation:
Introduction / Context:C++ defines special member functions to control copying, moving, construction, and destruction. When a new object must be initialized as a copy of an existing object, a particular special member is selected by the compiler. This question asks you to name that member precisely.
Given Data / Assumptions:
Concept / Approach:
The copy constructor initializes a new object from an existing object. Its canonical signature is T(const T&). The ordinary constructor builds objects from scratch (possibly with parameters). The destructor tears objects down at end of lifetime. The “default constructor” is the zero-argument constructor and is unrelated to copying. Recognizing which special member applies to each situation is core C++ knowledge for managing resource ownership and value semantics correctly.
Step-by-Step Solution:
Identify scenario: initialization from another object of the same type. Recall rule: the copy constructor is chosen for copy-initialization and direct copy construction. Confirm signature: T(const T&) (with possible additional move constructor overloads in modern C++). Select “copy constructor.”Verification / Alternative check:
Observe behavior when the copy constructor is deleted or private: the above forms of initialization become ill-formed, demonstrating the copy constructor’s role. Tools like compiler diagnostics explicitly mention “copy constructor is deleted.”
Why Other Options Are Wrong:
constructor: too general; does not specifically denote copying.
destructor: runs at end-of-life, not at initialization.
default constructor: creates objects without arguments, not from another object.
Common Pitfalls:
Final Answer:
copy constructor
Discussion & Comments