Do not carry away with the title “All about copy constructor in C++ with example”. In this article, I am not going to describe what copy constructor in C++ is. There is plenty of material available for that over the internet. Rather we will discuss why, where & how it used, how compiler synthesizes it for you and in what scenarios it called or not synthesized.
Why: Copy Constructor
The simple answer is to copy the data within the same data types, but if you want more concrete idea then see below image:
Note: Color represents assembly generated by the compiler for a corresponding C expression
- As you can see, for creating a copy within the same primitive data types like
char
,int
,float
,long double
, etc. compiler has special instructions. - But when you declare user-defined data type i.e. class/struct. The compiler does not have instruction for that because you are the owner of your type, so you have to define the copy operation in special member function called copy constructor suggesting compiler the way you want to copy.
- Whenever the situation of copy occurs compiler will simply substitute that expression with your copy constructor method.
Where: Copy Constructor
- Object initialization by another object of the same class/type.
- When an object passed as an argument to a function
- When a function returns a class object
Not: Copy Constructor
- The compiler will not synthesize copy constructor if class having a base class or sub-object with the deleted copy constructor.
- In this case, derived class copy constructor will be deleted implicitly. See below error for reference:
|
|
- Same stands true for move constructor & copy/move assignment operator.
How: Bitwise Copy & Memberwise Copy
- If class
word
defined as follows, then it exhibits bitwise copy constructor as the initialization ofverb
need not result in a function call.
- But, if the class
word
defined as follows, then it exhibits memberwise copy constructor & result in a function call because string declares explicit copy constructor.
- In this case, the compiler needs to synthesize a copy constructor as follows in order to invoke the copy constructor of the member class
string
object:
Not: Bitwise Copy Semantics..!
When bitwise copy semantics not exhibited by a class? There are four instances:
- When the class contains a member object of a class for which a copy constructor exists (either explicitly declared by the class designer, as in the case of the previous
string
class, or synthesized by
the compiler, as in the case of classWord
) - When the class derived from a base class for which a copy constructor exists (again, either explicitly
declared or synthesized) - When the class declares one or more virtual functions
- When the class derived from an inheritance chain in which one or more virtual base classes exist
In instances 1 & 2, the implementation needs to insert invocations of the member or base class copy constructors inside the synthesized copy constructor.
Other Ways: Can Copy Constructor Be Invoked
- All these statements transform into the invocation of the copy constructor.