类和对象(2)之类的6个默认成员函数(2)

在这里插入图片描述
上次我们梳理了初始化和清理的知识点,今天我们要梳理的是拷贝赋值的知识点。

拷贝构造函数

看到拷贝构造函数这个名字就能看的出来它是一个构造函数,所以它的语法和构造函数很相似。
既然他是一个构造函数,那么他就具有构造函数的语法,这里我们就不在赘述了。

特征

1.拷贝构造函数只有一个形参,该形参用const修饰,且必须是引用,可以自动调用

提问:为什么必须是引用?
在c++规定中,传值传参必须要调用拷贝构造。所以当你Date(Date d);是传值传参,那么就会先去调用拷贝构造,然后Date(Date d);就是拷贝构造,又会调用,然后继续调用拷贝构造,就会陷入一个死循环。(这里编译器直接不会编译通过,防止出现死递归)
在这里插入图片描述

2.编译器会自动生成拷贝构造函数,并且会处理内置类型,如果是自定义类型会去调用自定义类型中的拷贝构造函数。
注:浅拷贝可以用,深拷贝不能用!

#include<iostream>
using namespace std;
class Date
{
public:
	Date(int year=1, int month=1, int day=1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	/*Date(const Date& d)
	{
		this->_day = d._day;
		this->_month = d._month;
		this->_year = d._year;
	}*/
	
	void Print()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};
int main()
{
	Date d1(2024,1,29);
	Date d2(d1);
	d1.Print();
	d2.Print();
	return 0;
}

在这里插入图片描述
上述代码可见,我已经将我写的拷贝构造函数屏蔽,但是运行结果依然拷贝成功,证明编译器自动生成的拷贝构造函数是会处理的

3.用同类型对象初始化要创建的对象。

赋值重载

运算符重载

格式:返回类型 operator + 运算符号

规定
1.重载操作符必须有一个类类型的(自定义)参数
2.用于内置类型的运算符,其含义不能改变,例如:内置的整型+,不 能改变其含义
3.作为类成员函数重载时,其形参看起来比操作数数目少1,因为成员函数的第一个参数为隐藏的this。
4. .* :: sizeof ?: . 注意以上5个运算符不能重载。

#include<iostream>
using namespace std;
class Date
{
public:
	Date(int year=1, int month=1, int day=1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	bool operator < (const Date& d)
	{
		if (this->_year < d._year)
		{
			return true;
		}
		else  if(this->_year == d._year)
		{
			if (this->_month < d._month)
			{
				return true;
			}
			else if (this->_month == d._month)
			{
				if (this->_day < d._day)
				{
					return true;
				}
			}
		}
		return false;
	}
	
	void Print()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};
int main()
{
	Date d1(2024, 1, 29);
	Date d2(2024, 1, 28);
	cout << (d1 < d2) << endl;
	
	return 0;
}

在这里插入图片描述
会发现返回的是0,也就是说大于。

赋值重载

赋值重载就是将赋值运算符进行重载。两个已经存在的对象,一个拷贝赋值给另一个。(这里要与拷贝构造函数进行区分,一个是用同类型对象初始化要创建的对象,一个是两个已经存在的对象。)

1.多个类类型进行复制需要有返回值且返回值需要加&(引用)。
2.赋值重载会自动生成,且也会对内置类型处理。(深拷贝还是需要自己写!)
3.复制重载不能重载成全局函数!!!

#include<iostream>
using namespace std;
class Date
{
public:
	Date(int year=1, int month=1, int day=1)
	{
		_year = year;
		_month = month;
		_day = day;
	//d1=d2
	Date& operator= (const Date& d)
	{
		this->_day = d._day;
		this->_month = d._month;
		this->_year = d._year;

		return *this;
	}
	void Print()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};
int main()
{
	Date d1;
	Date d2;
	Date d3(2024, 1, 29);
	d1 = d2 = d3;
	return 0;
}

在这里插入图片描述
原因是因为连续赋值是从右向左开始的,也就是说从d2=d3开始,需要有返回值在赋值给d1,所以这个时候我们需要加引用。

  • 9
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值