C++ | C++ 类 & 对象 | C++ 拷贝构造函数

拷贝构造函数是一种特殊的构造函数,它在创建对象时,是使用同一类中之前创建的对象来初始化新创建的对象。拷贝构造函数通常用于:

  • ​通过使用另一个同类型的对象来初始化新创建的对象​​。
  • ​复制对象把它作为参数传递给函数​​。
  • ​复制对象,并从函数返回这个对象​​。

如果在类中没有定义拷贝构造函数,编译器会自行定义一个。如果类带有指针变量,并有动态内存分配,则它必须有一个拷贝构造函数。拷贝构造函数的最常见形式如下:

classname (const classname &obj) {
// 构造函数的主体
}

obj

实例1:

/*******************************************************************
* > File Name: classCopyConstructor.cpp
* > Create Time: 2021年09月 3日 9:01:07
******************************************************************/
#include <iostream>
using namespace std;

class Line{
public:
int getLength(void);
Line(int len); // 构造函数
Line(const Line &obj); // 拷贝构造函数
~Line(); // 析构函数
private:
int *ptr;
};

int Line::getLength(void)
{
return *ptr;
}

Line::Line(int len)
{
cout << "Calling the constructor " << endl;
ptr = new int; /* 为指针分配内存 */
*ptr = len;
}

Line::Line(const Line &obj)
{
cout << "Calling the constructor and allocation memory " << endl;
ptr = new int; /* 为指针分配内存 */
*ptr = *(obj.ptr); // 拷贝值
}

Line::~Line(void)
{
cout << "Free the memory" << endl;
delete ptr;
}

void display(Line obj)
{
//cout << "The length of line: " << *(obj.ptr) << endl; // ERR:‘int* Line::ptr’ is private within this context
cout << "The length of line: " << obj.getLength() << endl;
}

int main(void)
{
Line line(10);
display(line);

return 0;
}

编译、运行:

PS E:\fly-prj\cplusplus\day5> make
g++ -o classCopyConstructor classCopyConstructor.cpp -g -Wall
PS E:\fly-prj\cplusplus\day5> .\classCopyConstructor.exe
Calling the constructor
Calling the constructor and allocation memory
The length of line: 10
Free the memory
Free the memory

实例2:

通过使用已有的同类型的对象来初始化新创建的对象

/*******************************************************************
* > File Name: classCopyConstructor1.cpp
* > Create Time: 2021年09月 3日 9:25:59
******************************************************************/
#include <iostream>
using namespace std;

class Line{
public:
int getLength(void);
Line(int len); // 简单的构造函数
Line(const Line &obj); // 拷贝构造函数
~Line(); // 析构函数
private:
int *ptr;
};

int Line::getLength(void)
{
return (*ptr);
}

Line::Line(int len)
{
cout << "Calling the constructor func" << endl;
ptr = new int;
*ptr = len;
}

Line::Line(const Line &obj)
{
cout << "Calling the constructor func and allocate memory" << endl;
ptr = new int;
*ptr = *obj.ptr; //拷贝值
}

Line::~Line(void)
{
cout << "Free memory" << endl;
delete ptr;
}

void display(Line obj)
{
cout << "The length of line: " << obj.getLength() << endl;
}

int main(void)
{
Line line1(10);
cout << "1.===================" << endl;
Line line2 = line1; // 调用构造函数
cout << "2.===================" << endl;
display(line1);
cout << "3.===================" << endl;
display(line2);

return 0;
}

编译、运行:

PS E:\fly-prj\cplusplus\day5> make 
g++ -o classCopyConstructor1 classCopyConstructor1.cpp -g -Wall
PS E:\fly-prj\cplusplus\day5> .\classCopyConstructor1.exe
Calling the constructor func
1.===================
Calling the constructor func and allocate memory
2.===================
Calling the constructor func and allocate memory
The length of line: 10
Free memory
3.===================
Calling the constructor func and allocate memory
The length of line: 10
Free memory
Free memory
Free memory