C++ Copy constructor

C++ Classes & Objects

Copy constructorIt is a special constructor that, when creating an object, uses a previously created object of the same class to initialize the newly created object. The copy constructor is usually used for:

  • By using another object of the same type to initialize the newly created object.

  • Copy the object and pass it as a parameter to the function.

  • Copy the object and return the object from the function.

If a copy constructor is not defined in the class, the compiler will define one itself. If the class has pointer variables and dynamic memory allocation, then it must have a copy constructor. The most common form of the copy constructor is as follows:

classname (const classname &obj) { // Body of the constructor }

Here,objIt is an object reference, and this object 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 the constructor" << endl; // Allocate memory for the pointer ptr = new int; *ptr = len; } Line::Line(const Line &obj) { cout << "Call the copy constructor and allocate memory for pointer ptr" << endl; ptr = new int; *ptr = *obj.ptr; // copy value } Line::~Line(void) { cout << "Free memory" << endl; delete ptr; } int Line::getLength( void ) { return *ptr; } void display(Line obj) { cout << "line size :" << obj.getLength() <<endl; } // The 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 results:

调用构造函数
调用拷贝构造函数并为指针 ptr 分配内存
line 大小 : 10
释放内存
释放内存

The following example slightly modifies the above example by using an existing object of the same type to initialize a newly created 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 the constructor" << endl; // Allocate memory for the pointer ptr = new int; *ptr = len; } Line::Line(const Line &obj) { cout << "Call the copy constructor and allocate memory for pointer ptr" << endl; ptr = new int; *ptr = *obj.ptr; // copy value } Line::~Line(void) { cout << "Free memory" << endl; delete ptr; } int Line::getLength( void ) { return *ptr; } void display(Line obj) { cout << "line size :" << obj.getLength() <<endl; } // The main function of the program int main( ) { Line line1(10); Line line2 = line1; // The copy constructor is also called here. display(line1); display(line2); return 0; }

When the above code is compiled and executed, it produces the following results:

调用构造函数
调用拷贝构造函数并为指针 ptr 分配内存
调用拷贝构造函数并为指针 ptr 分配内存
line 大小 : 10
释放内存
调用拷贝构造函数并为指针 ptr 分配内存
line 大小 : 10
释放内存
释放内存
释放内存

C++ Classes & Objects

other extensions