C++中的赋值运算符重载和类的那点事

1.引言

在c++中我们有时可能需要对类对象,进行算术运算和逻辑运算等操作,但是对于自定义类型的对象来说默认情况C++是不支持使用运算符的,如下面的日期类Date中所存在的问题,此时C++中引入了运算符重载,来解决这一问题。

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

private:
	int _year;
	int _month;
	int _day;
};
int main()
{
	Date d1(2023,9,25);
	Date d2(2022,9,25);
	d1>d2;//此时这里会直接报错
	return 0;
}

2.语法规则

  • 函数名字为:关键字operator后面接需要重载的运算符符号。
  • 函数原型:返回值类型 operator操作符(参数列表)
  • 特点规则:
    1、不能通过连接其他符号来创建新的操作符:比如operator@
    2、重载操作符必须有一个类类型或者枚举类型的操作数
    3、用于内置类型的操作符,其含义不能改变,例如:内置的整型+,不 能改变其含义
    4、作为类成员的重载函数时,其形参看起来比操作数数目少1成员函数的
    操作符有一个默认的形参this,限定为第一个形参
    5、* 、:: 、sizeof 、?: 、. 注意以上5个运算符不能重载。
  • 使用运算符重载解决上面日期类Date中的问题
class Date
{
public:
	Date(int year = 0, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	int _year;
	int _month;
	int _day;
};

 函数名 operator操作符
 返回类型 看操作符运算后返回值是什么
 参数,操作符有几个操作数,他就有几个参数
bool operator>(const Date& d1, const Date& d2)
{
	if (d1._year > d2._year)
	{
		return true;
	}
	else if (d1._year == d2._year && d1._month > d2._month)
	{
		return true;
	}
	else if (d1._year == d2._year && d1._month == d2._month && d1._day > d2._day)
	{
		return true;
	}
	else
	{
		return false;
	}
}

int main()
{
	Date d1(2022, 1, 16);
	Date d2(2022, 1, 31);
	d1 > d2; // -》operator>(d1, d2)
	cout << operator>(d1, d2) << endl;

	return 0;
}
  • 上面的代码虽然解决了自定义类型的运算符问题,但是细心的同学可能会发现一些问题。对于这个重载运算符函数是写在类的外面的,为了能访问到类里的对象,我这里不得以把成员变量定义成了public,当然你也可以采用一些接口提供。但是这些方法都使问题变得复杂,并且失去了原有的类的封装的特性。哪还有什么别的解决方法吗?那就是在类中进行运算符的重载!!!

3.在类中进行运算符重载时的语法

话不多说,以下面一段代码为例。

class Date
{
public:
	Date(int year = 0, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	bool operator>(const Date& d) 
// bool operator>(Date* const this, const Date& d)
	{
		if (_year > d._year)
		{
			return true;
		}
		else if (_year == d._year && _month > d._month)
		{
			return true;
		}
		else if (_year == d._year && _month == d._month && _day > d._day)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
private:
	int _year;
	int _month;
	int _day;
};
int main()
{
	Date d1(2022, 1, 16);
	Date d2(2022, 1, 31);
	d1 > d2; // d1.operator>(d2);
	d1.operator>(d2);
	return 0;
}

有些同学一定会问类中的bool operator>(const Date& d) 怎么只有一个参数了,以前不是有两个参数吗?大家是否还记得我写过的详谈C++this指针一文,在类中是有一个隐藏的指针的,这里你看到的函数只有一个参数,但实际上是有两个参数的,隐藏的this指针和const Date& d。可以把类中的bool operator>(const Date& d)方法翻译成如下代码,来加深大家的理解。

 
 	bool operator>(Date* const this, const Date& d)
	{
		if (this->_year > d._year)
		{
			return true;
		}
		else if (this->_year == d._year && this->_month > d._month)
		{
			return true;
		}
//看到这里想必大家恍然大悟了吧!!!

4.赋值运算符重载!!!

4.1认识赋值运算符重载

请问下面的两种赋值运算符重载的方法A,B。你认为哪一种更好。

class Date
{ 
public :
 Date(int year = 1900, int month = 1, int day = 1)
 {
 _year = year;
 _month = month;
 _day = day;
 }
 //A
 Date (const Date& d)
 {
 	_year = d._year;
 	_month = d._month;
 	_day = d._day;
 	return *this;
 }
 //B
 Date& operator=(const Date& d)
 {
 	//防止进行自身赋值。如d1=d1这个情况
 	if(this != &d)
 	{
 		_year = d._year;
 		_month = d._month;
 		_day = d._day;
	}
	return *this;
 }
private:
 int _year ;
 int _month ;
 int _day ;
};

答案是B的写法更好,两种方法虽然都可以完成赋值运算符重载,但是在A中的写法在返回值的时候会调用拷贝构造,频繁的调用拷贝构造是存在效率问题的。

4.2赋值运算符重载的特点

  1. 参数类型
  2. 返回值(一般是返回引用)
  3. 检测是否自己给自己赋值
  4. 返回*this
  5. 一个类如果没有显式定义赋值运算符重载,编译器也会生成一个,完成对象按字节序的值拷贝。
  6. 编译器默认生成赋值重载的特点:内置类型成员,会完成字节序值拷贝–浅拷贝。自定义类型的成员变量,会表用它本身的operator=。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Jiawen_captial

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

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

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

打赏作者

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

抵扣说明:

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

余额充值