【C++ 类和对象】拷贝构造函数 ( •̀ ω •́ )

1. 类的6个默认成员函数🔍

在这里插入图片描述



2. 拷贝构造函数的概念🖊

在这里插入图片描述


3. 拷贝构造函数的特征🖊

🐧① 拷贝构造函数是构造函数的一个重载形式。

🐧② 内置类型完成值拷贝,自定义类型调用对应的拷贝构造和赋值重载



C++自定义的类型会调用它的拷贝构造;

在这里插入图片描述



🐧③ 拷贝构造函数的参数只有一个且必须是类类型对象的引用,使用传值方式编译器直接报错,因为会引发无穷递归调用。
在这里插入图片描述

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);
	 
	// 注意: Date d2 = d1;	这样写也是拷贝构造噢
	 return 0;
}

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

class Time
{
public:
	Time()
	{
		_hour = 1;
		_minute = 1;
		_second = 1;
	}
	Time(const Time& t)
	{
		_hour = t._hour;
		_minute = t._minute;
		_second = t._second;
		cout << "Time::Time(const Time&)" << endl;
	}
private:
	int _hour;
	int _minute;
	int _second;
};
class Date
{
private:
	// 基本类型(内置类型)
	int _year = 1970;
	int _month = 1;
	int _day = 1;
	// 自定义类型
	Time _t;
};
int main()
{
	Date d1;

	// 用已经存在的d1拷贝构造d2,此处会调用Date类的拷贝构造函数
	// 但Date类并没有显式定义拷贝构造函数,则编译器会给Date类生成一个默认的拷贝构造函数

	Date d2(d1);

	return 0;

}

注意:在编译器生成的默认拷贝构造函数中,内置类型是按照字节方式直接拷贝的,而自定
义类型是调用其拷贝构造函数完成拷贝的。



4. 那还需要我们显示拷贝构造吗 ?🍎

🐧 编译器生成的默认拷贝构造函数已经可以完成字节序的值拷贝了,还需要自己显式实现吗? 当然像日期类这样的类是没必要的。

🍎 那么下面的类呢?验证一下试试?

// 这里会发现下面的程序会崩溃掉?这里就需要我们以后讲的深拷贝去解决。
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;
 }
 
 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;
}

ff

注意:类中如果没有涉及资源申请时,拷贝构造函数是否写都可以;一旦涉及到资源申请时,则拷贝构造函数是一定要写的,否则就是浅拷贝。



5. 拷贝构造函数典型调用场景🖊

  • 🍎① 使用已存在对象创建新对象
  • 🍎② 函数参数类型为类类型对象
  • 🍎③ 函数返回值类型为类类型对象
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;
}

在这里插入图片描述

在这里插入图片描述

  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

未来可期LJ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值