C++初阶——运算符重载

前言:前面介绍过了函数重载,C++为了增强代码的可读性引入了运算符重载的概念,运算符重载是具有特殊函数名的函数,也具有其返回值类型。

下文博主将通过自定义类型日期类的比较引出运算符重载,以此凸显运算符重载提高代码可读性的优点。

目录

自定义类型比较

以整型和日期类为例:我们定义两个整型变量i,j,可以通过<比较它们的大小,但是我们实例化两个日期类对象d1,d2,如果我们想要比较它们的大小,该怎么做?

答:可以写一个比较Date的成员函数!

cpp 复制代码
class Date {
public:
	Date(int year = 1, int month = 1, int day = 1)
	{
		cout << "Date()" << endl;
		_year = year;
		_month = month;
		_day = day;
	}
//private:
	int _year;
	int _month;
	int _day;
};
bool Dateless(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()
{
	int i = 10, j = 20;
	Date d1(2023, 8, 12);
	Date d2(2022, 8, 12);
	cout << Dateless(d1,d2) << endl;
	//i < j; // 语言自带的类型
	//d1 < d2; //自定义类型
	return 0;
}

但是Dateless不能直观地让使用者知道程序设计的是前者大还是后者大。我们可不可以使用运算符比较自定义类型?写成d1<d2这样的形式?

运算符重载

我们定义了日期类,编译器无法比较它们的大小,但是我们可以通过运算符重载来重新定义运算符的作用,以达到是运算符对自定义类型也有效的目的。
格式:operator(运算符)

cpp 复制代码
class Date {
public:
	Date(int year = 1, int month = 1, int day = 1)
	{
		cout << "Date()" << endl;
		_year = year;
		_month = month;
		_day = day;
	}
//private:
	int _year;
	int _month;
	int _day;
};
//<运算符重载
//d1<d2
//operator(d1<d2)
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(2023, 8, 12);
	Date d2(2022, 8, 12);
	cout << (d1 < d2) << endl; // 直接通过<调用operator<函数
	cout << operator<(d1, d2) << endl; //是上一行的显示调用

通过运算符重载,我们可以直接写成d1<d2这样的形式,就像比较内置类型一样,让使用者一目了然。

1.使用注意事项

1.不能通过连接不是运算符的符号来创建新的操作符,比如:operator¥

2.重载操作符必须有一个类类型参数

3.用于内置类型的运算符,其含义不能改变。

4.作为类成员函数重载时,其形参看起来比操作数数目少1,因为成员函数的第一个参数为隐藏的this

5.以下5个运算符不能重载:. ?: sizeof :: .*

2.作为类的成员函数重载

作为类成员函数重载时,其形参看起来比操作数数目少1,因为成员函数的第一个参数为隐藏的this(对应注意事项的第四点)

上面的示例代码operator<函数是全局函数,当我们面对不同的自定义类型需要定义多个<运算符重载时,需要将operator<函数放入日期类中。

运算符有几个操作数,运算符重载函数就有几个参数 以<为例,它有两个操作数,因此拥有两个参数,但是放在类中我们只要写一个参数,因为成员函数都隐藏了一个this指针参数

cpp 复制代码
class Date {
public:
	Date(int year = 1, int month = 1, int day = 1)
	{
		cout << "Date()" << endl;
		_year = year;
		_month = month;
		_day = day;
	}
   //作为类的成员函数重载
   //d1<d2
   //d1.operator<(d2)
	bool operator<(const Date& d)  //(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()
{
	int i = 10, j = 20;
	Date d1(2023, 8, 12);
	Date d2(2022, 8, 12);
	cout << (d1 < d2) << endl; 
	cout << d1.operator<(d2) << endl;  // 显示调用方式与全局中的不同

总结:运算符重载也可以被普通函数替代(就像例子所示我们仅仅是修改了函数的名字),但是使用运算符重载可以让我们的程序可读性大大提升。

相关推荐
李少兄2 分钟前
JavaScript 对象完全指南
开发语言·javascript·ecmascript
张小姐的猫5 分钟前
【AI大模型接入SDK】 —— Ollama本地接入Deepseek
java·linux·开发语言·网络·c++·人工智能
Nuanyt7 分钟前
JUC常见核心知识梳理01 线程 并发 JMM volatile 管程 锁 synchronized
java·开发语言·网络·jvm
adaierya15 分钟前
用 AI 解决音频转换编程问题
开发语言·人工智能·python·分类·ai编程
何何____16 分钟前
web方向js考核题解
开发语言·前端·javascript
wabs66619 分钟前
关于二叉树【力扣116.填充每个节点的下一个右侧节点指针的思考】
数据结构·c++·算法·leetcode·二叉树·层序遍历
ThornArmor19 分钟前
重铸1996|首秀点亮:编写 helloworld 并用 Makefile 自动化收割第一个 .z64 卡带镜像
开发语言
船厂电气自动化ai大模型21 分钟前
AI大模型与数学 第73课 秩的核心定义、矩阵秩的通用求解方法
开发语言·数据结构·机器学习
小白学大数据1 小时前
Codex 里的 GPT 6 Astra、GPT 5.6 Sol、Terra、Luna 怎么选
开发语言·gpt·microsoft
l1t3 小时前
DeepSeek 4.1总结的Tom Lane 谈塑造 Postgres 三十年历程的架构决策
开发语言·数据库·postgresql·架构