当前位置: 首页 > news >正文

【C++】类和对象之拷贝构造函数篇

个人主页 : zxctscl
文章封面来自:艺术家–贤海林
如有转载请先通知

文章目录

  • 1. 前言
  • 2. 传值传参和传引用传参
  • 3. 概念
  • 4. 特征

1. 前言

在前面学习了6个默认成员函数中的构造函数和析构函数 【C++】构造函数和析构函数详解,接下来继续往后看拷贝构造函数。

拷贝构造函数就是用一个同类型的其他对象来构造。
要学习拷贝构造函数,得先了解传值传参和传引用传参。

2. 传值传参和传引用传参

class Date
{
public:
	Date(int year = 1, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}

	Date(Date& d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}

	void Print()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}

private:
	// 内置类型
	int _year;
	int _month;
	int _day;
};
void func1(Date d)
{

}
void func2(Date& rd)
{

}
int main()
{
	Date d1(2024, 2, 24);
	func1(d1);
	func2(d1);

	return 0;
}

C++规定自定义类型都会调用拷贝构造。
在这里插入图片描述
所以func2(d1);rd是d1的别名,直接完成调用了,不存在再有一个函数来传值传参。
在这里插入图片描述
在这里插入图片描述
来看看不传引用会怎么样

class Date
{
public:
	Date(int year = 1, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}


	Date(Date d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}

	void Print()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}

private:
	
	int _year;
	int _month;
	int _day;
};

int main()
{
	Date d1(2024, 2, 24);
	Date d2(d1);
	return 0;
}

在这里插入图片描述
调用拷贝构造,要先传参,这里是传值,传参,会形成一个新的拷贝构造,进不来这个函数,一直递归下去。使用传值传参就不行。

3. 概念

在现实生活中,可能存在一个与你一样的自己,我们称其为双胞胎。

在这里插入图片描述
那在创建对象时,可否创建一个与已存在对象一某一样的新对象呢?
拷贝构造函数:只有单个形参,该形参是对本类类型对象的引用(一般常用const修饰),在用已存在的类类型对象创建新对象时由编译器自动调用。

4. 特征

拷贝构造函数也是特殊的成员函数,其特征如下:

  1. 拷贝构造函数是构造函数的一个重载形式。
  2. 拷贝构造函数的参数只有一个且必须是类类型对象的引用,使用传值方式编译器直接报错,因为会引发无穷递归调用。

有时候可能会发生修改对象,为了保护对象,就可以在它前面加上const
是一种权限的缩小。

像下面的场景就能被检查出来:
在这里插入图片描述
就只能这样写:
在这里插入图片描述
在这里插入图片描述

  1. 若未显式定义,编译器会生成默认的拷贝构造函数。 默认的拷贝构造函数对象按内置类型成员内存存储按字节序完成拷贝,这种拷贝叫做浅拷贝,或者值拷贝。

像下面的代码中就没有拷贝构造:

class Date
{
public:
	Date(int year = 1, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}

	void Print()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}

private:
	// 内置类型
	int _year;
	int _month;
	int _day;

	
};

int main()
{
	Date d1(2024, 2, 24);
	Date d2(d1);
	d1.Print();
	d2.Print();

	return 0;
}

在这里插入图片描述

class Time
{
public:
	~Time()
	{
		cout << "~Time()" << endl;
	}

	// 强制编译器生成
	Time() = default;

	Time(const Time& t)
	{
		cout << "Time(const Time& t)" << endl;
		_hour = t._hour;
		_minute = t._minute;
		_second = t._second;
	}

private:
	int _hour;
	int _minute;
	int _second;
};

class Date
{
public:
	Date(int year = 1, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}

	void Print()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}

private:
	// 内置类型
	int _year;
	int _month;
	int _day;

	//自定义类型
	Time _t;
	
};

int main()
{
	Date d1(2024, 2, 24);
	Date d2(d1);
	d1.Print();
	d2.Print();

	return 0;
}

在这里插入图片描述
在编译器生成的默认拷贝构造函数中,内置类型是按照字节方式直接拷贝的,而自定
义类型是调用其拷贝构造函数完成拷贝的。

在这里插入图片描述

  1. 编译器生成的默认拷贝构造函数已经可以完成字节序的值拷贝了,还需要自己显式实现吗?
typedef int DataType;
class Stack
{
public:
	Stack(size_t capacity = 10)
	{
		_array = (DataType*)malloc(capacity * sizeof(DataType));
		if (nullptr == _array)
		{
			perror("malloc申请空间失败");
			return;
		}
		_size = 0;
		_capacity = capacity;
	}

	// Stack st2(st1);
	Stack(const Stack& s)
	{
		DataType* tmp = (DataType*)malloc(s._capacity *(sizeof(DataType)));
		if (tmp == nullptr)
		{
			perror("malloc fail");
			exit(-1);
		}

		memcpy(tmp, s._array, sizeof(DataType) * s._size);

		_array = tmp;
		_size = s._size;
		_capacity = s._capacity;
	}

	void Push(const DataType& data)
	{
		// CheckCapacity();
		_array[_size] = data;
		_size++;
	}

	~Stack()
	{
		if (_array)
		{
			free(_array);
			_array = nullptr;
			_capacity = 0;
			_size = 0;
		}
	}
private:
	DataType* _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置空,s1置空不影响s2置空,他们是两个对象。但他们的地址指向同一个空间,同一个空间不能释放两次。会导致野指针。
在这里插入图片描述
注意:类中如果没有涉及资源申请时,拷贝构造函数是否写都可以;一旦涉及到资源申请时,则拷贝构造函数是一定要写的,否则就是浅拷贝。
在这里插入图片描述

  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;
};
Date Test(Date d)
{
	Date temp(d);
	return temp;
}
int main()
{
	Date d1(2022, 1, 13);
	Test(d1);
	return 0;
}

在这里插入图片描述
为了提高程序效率,一般对象传参时,尽量使用引用类型,返回时根据实际场景,能用引用尽量使用引用。

有问题请指出,大家一起进步!!!

相关文章:

  • docker install private registry 【docker 安装 registry 仅证书认证】
  • mybatis中foreach批量插入并返回主键
  • oppo手机如何录屏?解锁录屏新功能!
  • SQL 中如何实现多表关联查询?
  • 【达梦数据库】数据库的方言问题导致的启动失败
  • 【pytorch】pytorch模型可复现设置
  • 5 buuctf解题
  • python 提取PDF文字
  • django自定义后端过滤
  • 【精选】网络安全大厂面试题 2.0
  • 物联网在智慧景区中的应用:提升游客体验与运营效率
  • 中国农业无人机行业市场现状分析与投资前景预测研究报告
  • This dependency was not found解决方法
  • 大数据之Flink优化
  • C 标准库 - <stdlib.h>
  • 抽象的java
  • 电路设计(28)——交通灯控制器的multisim仿真
  • 时间获取、文件属性获取 2月20日学习笔记
  • 【MySQL】数据类型——MySQL的数据类型分类、数值类型、小数类型、字符串类型
  • C# 实现网页内容保存为图片并生成压缩包
  • https://app.hackthebox.com/machines/Inject
  • Spring —— Spring简单的读取和存储对象 Ⅱ
  • 渗透测试之冰蝎实战
  • Mybatis、TKMybatis对比
  • Microsoft Office 2019(2022年10月批量许可版)图文教程
  • 《谷粒商城基础篇》分布式基础环境搭建
  • 哈希表题目:砖墙
  • Vue 3.0 选项 生命周期钩子
  • 【车载嵌入式开发】AutoSar架构入门介绍篇
  • 【计算机视觉 | 目标检测】DETR风格的目标检测框架解读