C++Copy Constructor
Copy Constructor is a special constructor that initializes a new object using an object of the same class that was created earlier. The copy constructor is typically used for:
- Initializing a new object with another object of the same type.
- Copying an object to pass it as an argument to a function.
- Copying an object to return it from a function.
If a copy constructor is not defined in a class, the compiler supplies its own copy constructor. If the class has pointer variables and has dynamic memory allocation, it must have a copy constructor. The most common form of the copy constructor is as follows:
classname (const classname &obj) {
// Constructor body
}
Here, obj is a reference to an object that is used to initialize another object.
Example
#include <iostream>
using namespace std;
class Line
{
public:
int getLength( void );
Line( int len ); // Simple constructor
Line( const Line &obj); // Copy constructor
~Line(); // Destructor
private:
int *ptr;
};
// Member function definitions, including constructor
Line::Line(int len)
{
cout << "Calling constructor" << endl;
// Allocate memory for the pointer
ptr = new int;
*ptr = len;
}
Line::Line(const Line &obj)
{
cout << "Calling copy constructor and allocating memory for pointer ptr" << endl;
ptr = new int;
*ptr = *obj.ptr; // Copy the value
}
Line::~Line(void)
{
cout << "Freeing memory" << endl;
delete ptr;
}
int Line::getLength( void )
{
return *ptr;
}
void display(Line obj)
{
cout << "line size : " << obj.getLength() <<endl;
}
// Main function of the program
int main( )
{
Line line(10);
display(line);
return 0;
}
When the above code is compiled and executed, it produces the following result:
Calling constructor
Calling copy constructor and allocating memory for pointer ptr
line size : 10
Freeing memory
Freeing memory
The following example modifies the above example slightly by initializing a new object using an existing object of the same type:
Example
#include <iostream>
using namespace std;
class Line
{
public:
int getLength( void );
Line( int len ); // Simple constructor
Line( const Line &obj); // Copy constructor
~Line(); // Destructor
private:
int *ptr;
};
// Member function definitions, including constructor
Line::Line(int len)
{
cout << "Calling constructor" << endl;
// Allocate memory for the pointer
ptr = new int;
*ptr = len;
}
Line::Line(const Line &obj)
{
cout << "Calling copy constructor and allocating memory for pointer ptr" << endl;
ptr = new int;
*ptr = *obj.ptr; // Copy the value
}
Line::~Line(void)
{
cout << "Freeing memory" << endl;
delete ptr;
}
int Line::getLength( void )
{
return *ptr;
}
void display(Line obj)
{
cout << "line size : " << obj.getLength() <<endl;
}
// Main function of the program
int main( )
{
Line line1(10);
Line line2 = line1; // This also calls the copy constructor
display(line1);
display(line2);
return 0;
}
return 0;
}
When the above code is compiled and executed, it produces the following result:
Calling constructor
Calling copy constructor and allocating memory for pointer ptr
Calling copy constructor and allocating memory for pointer ptr
line size: 10
Freeing memory
Calling copy constructor and allocating memory for pointer ptr
line size: 10
Freeing memory
Freeing memory
Freeing memory