Difficulty: Easy
Correct Answer: A copy constructor is a special constructor that initialises a new object as a copy of an existing object of the same class, usually taking a const reference parameter of that class type
Explanation:
Introduction / Context:
Copy constructors are a core part of C++ class design, used whenever objects are copied by value. Understanding what a copy constructor is and when it is called is essential for writing correct code, especially when classes manage resources such as dynamic memory. Many interview questions focus on copying behaviour and the rule of three or rule of five, starting with the definition of a copy constructor.
Given Data / Assumptions:
Concept / Approach:
A copy constructor is a special constructor that takes an existing object of the same class and uses it to initialise a new object. It is typically invoked in three cases: when an object is initialised from another object of the same type, when an object is passed by value to a function, and when an object is returned by value from a function. The canonical form uses a reference parameter, usually const, to avoid infinite recursion and to allow copying from const objects. The correct option must capture this idea without claiming that all copies are deep or that the constructor has no parameters.
Step-by-Step Solution:
Step 1: Recall the standard declaration: ClassName(const ClassName &other); where other is the source object.
Step 2: Recognise that this constructor is called when we write ClassName b = a; where a is an existing object.
Step 3: Remember that passing an object by value to a function or returning an object by value can also invoke the copy constructor.
Step 4: Examine option (a), which says that a copy constructor initialises a new object as a copy of an existing object of the same class and usually takes a const reference parameter.
Step 5: Confirm that this matches the standard textbook definition and select it as the answer.
Verification / Alternative check:
To verify, consider a simple class that prints a message in its copy constructor. Whenever you pass an instance by value to a function, you will see the message, indicating that the copy constructor was called to create the parameter object. Similarly, when a function returns an object by value, the copy constructor may be involved, depending on optimisation. These observations align with the conceptual description given in option (a).
Why Other Options Are Wrong:
Option (b) is wrong because the copy constructor does not necessarily create a deep copy; whether copying is shallow or deep depends on how the programmer implements it. Option (c) incorrectly describes a default constructor, not a copy constructor. Option (d) is incorrect because converting constructors that take different types are not copy constructors; they are used for type conversion rather than copying an object of the same class.
Common Pitfalls:
A common pitfall is to assume that the compiler generated copy constructor always performs a safe deep copy. In classes that manage resources such as dynamic memory, file handles, or mutexes, the default shallow copy can be dangerous. In such cases, programmers often implement a custom copy constructor, copy assignment operator, and destructor, following the rule of three or rule of five. Understanding the role and definition of the copy constructor is the first step in mastering these patterns.
Final Answer:
A copy constructor is a special constructor that initialises a new object as a copy of an existing object of the same class, usually taking a const reference parameter of that class type.
Discussion & Comments