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

前言:前面介绍过了函数重载,C++为了增强代码的可读性引入了运算符重载的概念,运算符重载是具有特殊函数名的函数,也具有其返回值类型。
下文博主将通过自定义类型日期类的比较引出运算符重载,以此凸显运算符重载提高代码可读性的优点

自定义类型比较

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

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(运算符)

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指针参数

扫描二维码关注公众号,回复: 16573522 查看本文章
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;  // 显示调用方式与全局中的不同

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

猜你喜欢

转载自blog.csdn.net/qq_73390155/article/details/132249714