【C++】C++ 运算符重载

运算符重载

C++为了增加代码的可读性引入了运算符重载,运算符重载是具有特殊函数名的函数,也具有其返回值类型,函数名字以及参数列表,其返回值类型与参数列表与普通函数类似
函数名字为: 关键字operator后面接需要重载的运算符符号
函数原型:返回值类型 operator操作符(参数列表)

注意:
不能通过链接其他符号来创建新的操作符:比如operator@
重载操作符必须有一个类类型参数
用于内置类型的运算符,其含义不能改变,例如:内置整型+,不能改变其含义
作为类成员函数重载时,其形参看起来比操作数数目少1,因为成员函数的第一个参数为隐藏的this
有5个运算符不能重载,“ .* ” , “ ::” , “sizeof” , “ ?:” , “ . ” 笔试题常考

1.日期类例子

运算符重载既可以设置为全局函数,也可以是成员函数

一. 全局函数

class Date
{
public:
	//构造函数
	Date(int year = 2023, int month = 2, int day = 4)
	{
		_year = year;
		_month = month;
		_day = day;
	}

	//拷贝构造函数
	Date(const Date&d1)
	{
		_year = d1._year;
		_month = d1._month;
		_day = d1._day;
	}
	
	int _year;
	int _month;
	int _day;
};

//全局的==运算符重载
bool operator==(const Date&d1, const Date&d2)
{
	return d1._year == d2._year
	&&d1._month == d2._month
	&&d1._day == d2._day;
}

int main()
{
	Date d1(2022,1,1);
	Date d2;
    
    //因为<<的优先级比==高,所以我们需要加个括号
	cout << (d1 == d2) << endl;
	//本质上行代码会被编译器转换成
	cout << operator==(d1, d2) << endl;
	
	return 0;
}

注意:如果运算符重载是全局变量,那么目前的知识,我们需要把类的成员变量设为共有,或者再在类内提供GetYear,GetMonth,GetDay等获取成员变量的成员函数。不然无法访问私有的成员变量

二. 成员函数

class Date
{
public:
	//构造函数
	Date(int year = 2023, int month = 2, int day = 4)
	{
		_year = year;
		_month = month;
		_day = day;
	}

	//拷贝构造函数
	Date(const Date&d1)
	{
		_year = d1._year;
		_month = d1._month;
		_day = d1._day;
	}

	//==运算符重载
	bool operator==(const Date&d)
	{
		return _year == d._year&&_month == d._month&&_day == d._day;
	}

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


int main()
{
	Date d1(2022,1,1);
	Date d2;
	
	//展开的本质
	cout<<d1.operator==(d2);
	cout << (d1 == d2) << endl;


	return 0;
}

注意:因为是成员函数,所以调用的时候,是d1对象调用成员函数,并且会有隐式this参数,所以成员函数的定义,只需要再有一个参数即可

2.运算符重载的复用性

还是日期类,我们实现一下d1<d2的判断

class Date
{
public:
	//构造函数
	Date(int year = 2023, int month = 2, int day = 4)
	{
		_year = year;
		_month = month;
		_day = day;
	}

	//拷贝构造函数
	Date(const Date&d1)
	{
		_year = d1._year;
		_month = d1._month;
		_day = d1._day;
	}

	//==运算符重载
	bool operator==(const Date&d)
	{
		return _year == d._year&&_month == d._month&&_day == d._day;
	}

	//d1<d2
	bool operator<(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;
};

此时,如果我们还需要增添>=,>,<=,!=的运算重载,就可以利用已经写好的<运算符和==运算符重载

    //d1<=d2
	bool operator<=(const Date&d)
	{
		return *this < d || *this == d;
	}

	//d1>d2
	bool operator>(const Date&d)
	{
		return !(*this <= d);
	}

	//d1>=d2
	bool operator>=(const Date&d)
	{
		return !(*this < d);
	}

	//d1!=d2
	bool operator!=(const Date&d)
	{
		return !(*this == d);
	}

3.赋值运算符重载

我们依然以日期类为例子

1.参数是因为函数内部不修改传参,所以加const。并且传引用可以减少拷贝构造
2.返回值为Date&,是因为要实现d3=d2=d1;

    //1.参数是因为函数内部不修改传参,所以加const。并且传引用可以减少拷贝构造
	//2.返回值为Date&,是因为要实现d3=d2=d1;
	Date&operator=(const Date&d)
	{
	    //可能会有d1=d1,所以可以加这个判断
		if (*this != d)
		{
			_year = d._year;
			_month = d._month;
			_day = d._day;

		}

		return *this;
	}


int main()
{
   Date d1(2022,1,1);
	Date d2;
	Date d3;

	d3 = d2 = d1;

	d3.Print();
	d2.Print();
	d1.Print();


	return 0;
}

在这里插入图片描述

小总结

在这里插入图片描述

1.我们不写,编译器会自动生成,我们写了,编译器就不会自动生成。
默认构造函数&析构函数:
a. 内置类型不做处理
b. 自定义类型会调用对应构造/析构

默认成拷贝构造和赋值重载:
a. 内置类型完成浅拷贝/值拷贝–按byte一个一个拷贝
b. 自定义类型,去调用这个成员拷贝构造/赋值重载

2.自动调用

本章用于记笔记,如果有不对或者不足的地方,欢迎大佬们指正,补充。感谢大家的阅读,如果感觉博主写的还可以,麻烦点个赞支持一下,阿里嘎多。在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值