cpp
Demo
#include <iostream>
#include <Windows.h>
#include <string>
#include <string.h>
using namespace std;
// 定义一个"人类"
class Human {
public:
Human();
Human(int age, int salary);
Human(const Human&); //不定义拷贝构造函数,编译器会生成"合成的拷贝构造函数"
void eat();
void sleep();
void play();
void work();
string getName();
int getAge();
int getSalary();
void setAddr(const char *newAddr);
const char* getAddr();
private:
string name = "Unknown";
int age = 28;
int salary;
char *addr;
};
Human::Human() {
name = "无名氏";
age = 18;
salary = 30000;
}
Human::Human(int age, int salary) {
cout << "调用自定义的构造函数" << endl;
this->age = age; //this是一个特殊的指针,指向这个对象本身
this->salary = salary;
name = "无名";
addr = new char[64];
strcpy_s(addr, 64, "China");
}
Human::Human(const Human &man) {
cout << "调用自定义的拷贝构造函数" << "参数:" << &man << " 本对象:" << this << endl;
age = man.age; //this是一个特殊的指针,指向这个对象本身
salary = man.salary;
name = man.name;
// 深度拷贝
addr = new char[64];
strcpy_s(addr, 64, man.addr);
}
void Human::eat() {
cout << "吃炸鸡,喝啤酒!" << endl;
}
void Human::sleep() {
cout << "我正在睡觉!" << endl;
}
void Human::play() {
cout << "我在唱歌! " << endl;
}
void Human::work() {
cout << "我在工作..." << endl;
}
string Human::getName() {
return name;
}
int Human::getAge() {
return age;
}
int Human::getSalary() {
return salary;
}
void Human::setAddr(const char *newAddr) {
if (!newAddr) {
return;
}
strcpy_s(addr, 64, newAddr);
}
const char* Human::getAddr() {
return addr;
}
void test(Human man) {
cout << man.getSalary() << endl;
}
void test2(Human &man) { //不会调用拷贝构造函数,此时没有没有构造新的对象
cout << man.getSalary() << endl;
}
Human test3(Human &man) {
return man;
}
Human& test4(Human &man) {
return man;
}
int main(void) {
Human h1(25, 35000); // 调用默认构造函数
Human h2(h1); // 调用拷贝构造函数
Human h3 = h1; // 调用拷贝构造函数
test(h1); // 调用拷贝构造函数
test2(h1); // 不会调用拷贝构造函数
test3(h1); // 创建一个临时对象,接收test3函数的返回值,调用1次拷贝构造函数
Human h4 = test3(h1); // 仅调用1次拷贝构造函数,返回的值直接作为h4的拷贝构造函数的参数
test4(h1); // 因为返回的是引用类型,所以不会创建临时对象,不会调用拷贝构造函数
Human men[] = { h1, h2, h3 }; //调用3次拷贝构造函数
system("pause"); return 0;
}
一、什么是拷贝构造函数
拷贝构造函数的标准形式:
cpp
Human(const Human& other);
作用:
用一个已有对象,创建一个新的对象
例如:
cpp
Human h1(25,35000);
Human h2(h1); // 用 h1 创建 h2
这里会调用:
Human(const Human& man)
二、什么时候会调用拷贝构造函数
你的笔记总结是正确的,主要有 4种情况。
1 对象初始化另一个对象
cpp
Human h1(25,35000);
Human h2(h1); // 调用
Human h3 = h1; // 也调用
注意:
cpp
Human h3 = h1;
不是赋值
而是
用 h1 初始化 h3
所以调用 拷贝构造函数
2 函数参数是对象(值传递)
函数:
cpp
void test(Human man)
调用:
cpp
test(h1);
执行过程:
h1 ----> 复制 ----> man
所以:
调用一次拷贝构造函数
如果是引用:
cpp
void test2(Human &man)
调用:
cpp
test2(h1);
执行:
h1 ------> 直接引用
没有创建新对象。
所以:
不会调用拷贝构造函数
3 函数返回对象
函数:
cpp
Human test3(Human &man) {
return man;
}
返回类型:
Human
调用:
cpp
test3(h1);
执行过程:
man ----> 复制 ----> 临时对象
所以:
调用拷贝构造函数
如果接收:
cpp
Human h4 = test3(h1);
现代编译器会优化(RVO):
man ----> h4
所以:
只调用1次拷贝构造
4 用对象初始化对象数组
代码:
cpp
Human men[] = { h1, h2, h3 };
等价于:
men[0] = h1
men[1] = h2
men[2] = h3
每个元素都要:
拷贝一次
所以:
调用3次拷贝构造函数
三、什么时候不会调用
1 引用参数
cpp
void test2(Human &man)
因为:
没有创建新对象
2 返回引用
cpp
Human& test4(Human &man)
返回:
man 的引用
所以:
不会产生临时对象
四、你的程序完整调用流程
假设运行 main:
cpp
Human h1(25, 35000);
输出
调用自定义的构造函数
cpp
Human h2(h1);
输出
调用自定义的拷贝构造函数
cpp
Human h3 = h1;
输出
调用自定义的拷贝构造函数
cpp
test(h1);
输出
调用自定义的拷贝构造函数
cpp
test2(h1);
不调用
cpp
test3(h1);
调用一次
cpp
Human h4 = test3(h1);
调用一次(编译器优化)
cpp
test4(h1);
不调用
cpp
Human men[] = { h1, h2, h3 };
调用3次
五、为什么要写拷贝构造函数(最重要)
因为你有:
cpp
char *addr;
如果不用 深拷贝
编译器默认拷贝:
addr = man.addr
结果:
两个对象指向同一块内存
后果:
释放内存时 double free
所以你写了:
cpp
addr = new char[64];
strcpy_s(addr,64,man.addr);
这叫:
深拷贝
六、一句话总结(考试必背)
拷贝构造函数调用的四种情况
1️⃣ 用对象初始化对象
2️⃣ 函数参数是对象(值传递)
3️⃣ 函数返回对象(非引用)
4️⃣ 用对象初始化对象数组
不会调用
- 引用参数
- 返回引用