引入
⛳️ 推荐
文章目录
一、拷贝构造函数的引入
1.1 拷贝构造的概念
如图所见拷贝构造函数是我们的六大成员默认函数之一,构造函数的作用是初始化,析构函数是复制清理工作,而我们的构造拷贝函数是用来同类对象进行赋值给另一个对象时的工作:
二、拷贝构造函数的特征
2.1 拷贝构造的书写形式
- 拷贝构造函数是构造函数的一个重载形式
- 拷贝构造函数的参数只有一个且必须是类类型对象的引用,使用传值方式编译器直接报错,
因为会引发无穷递归调用。
🍸 代码演示:
class Date
{
public:
Date(int year = 1900, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
// Date(const Date d) // 错误写法:编译报错,会引发无穷递归
Date(const Date& d) // 正确写法
{
_year = d._year;
_month = d._month;
_day = d._day;
}
private:
int _year;
int _month;
int _day;
};
int main()
{
Date d1;
Date d2(d1);
return 0;
}
- 所以我们在书写构造函数的时候一定要使用传引用当做形参
2.2 不显示定义自动创建
🍸 代码演示:
🔥 这是因为默认生成的拷贝构造函数完成的只是浅拷贝,只是把值复制过去了
2.3 浅拷贝与深拷贝
🍸 代码演示:
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<stdlib.h>
using namespace std;
class Stack
{
public:
Stack(size_t capacity = 10)
{
_array = (int*)malloc(capacity * sizeof(int));
if (nullptr == _array)
{
perror("malloc申请空间失败");
return;
}
_size = 0;
_capacity = capacity;
}
void Push(const int& data)
{
// CheckCapacity();
_array[_size] = data;
_size++;
}
~Stack()
{
if (_array)
{
free(_array);
_array = nullptr;
_capacity = 0;
_size = 0;
}
}
private:
int* _array;
size_t _size;
size_t _capacity;
};
int main()
{
Stack s1;
s1.Push(1);
s1.Push(2);
s1.Push(3);
s1.Push(4);
Stack s2(s1);
return 0;
}
这里就是我们说的浅拷贝了,为什么程序回出现崩溃呢?这里刚开始创建了一个S1 对象,又创建了一个S2 对象去进行调用拷贝构造函数进行拷贝:
- 而这里只进行了浅拷贝,在 S2 进行拷贝构造的时候只是简单的把值复制过去了
- 所以 S2 和 S1 是指向同一块空间并没有给 S2 去单独开辟一个一模一样的空间
🔥 所以在这些去动态申请资源的函数的类去,一定要显示定义拷贝构造函数进行深拷贝
- 把空间大小和值(内容)完全拷贝进去
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<stdlib.h>
using namespace std;
class Stack
{
public:
Stack(size_t capacity = 10)
{
_array = (int*)malloc(capacity * sizeof(int));
if (nullptr == _array)
{
perror("malloc申请空间失败");
return;
}
_size = 0;
_capacity = capacity;
}
Stack(const Stack& p1)
{
int* tmp = (int*)malloc(sizeof(int) * p1._capacity);
if (tmp == nullptr)
{
perror("file malloc");
exit(-1);
}
memcpy(tmp, p1._array, sizeof(int) * p1._size);
_array = tmp;
_capacity = p1._capacity;
_size = p1._size;
}
void Push(const int& data)
{
// CheckCapacity();
_array[_size] = data;
_size++;
}
~Stack()
{
if (_array)
{
free(_array);
_array = nullptr;
_capacity = 0;
_size = 0;
}
}
private:
int* _array;
size_t _size;
size_t _capacity;
};
int main()
{
Stack s1;
s1.Push(1);
s1.Push(2);
s1.Push(3);
s1.Push(4);
Stack s2(s1);
return 0;
}
2.4 不申请资源不需要显示定义
- 🔥 注:类中如果没有涉及资源申请时,拷贝构造函数是否写都可以;一旦涉及到资源申请
时,则拷贝构造函数是一定要写的,否则就是浅拷贝。
三、拷贝构造函数调用场景
3.1 使用已存在对象创建新对象
🍸 代码演示:
class Date
{
public:
Date(int year, int minute, int day)
{
cout << "Date(int,int,int):" << this << endl;
}
Date(const Date& d)
{
cout << "Date(const Date& d):" << this << endl;
}
~Date()
{
cout << "~Date():" << this << endl;
}
private:
int _year;
int _month;
int _day;
};
int main()
{
Date d1(2022,1,13);
Test(d1);
return 0;
}
3.2 函数参数类型为类类型对象
在以前学习函数的时候我们知道,形参是实参的一份临时拷贝所以当函数参数类型为 类 的类型对象的话也会自动调用 拷贝构造函数
🍸 代码演示:
Date Test(Date d)
{
Date temp(d);
return temp;
}
3.3 函数返回值类型为类类型对象
🍸 代码演示:
Date Test(Date d)
{
Date temp(d);
return temp;
}
📝文章结语:
☁️ 看到这里了还不给博主扣个:
⛳️ 点赞
🍹收藏
⭐️ 关注
!
💛 💙 💜 ❤️ 💚💓 💗 💕 💞 💘 💖
拜托拜托这个真的很重要!
你们的点赞就是博主更新最大的动力!