一、什么是运算符重载
C++中的运算符重载(Operator Overloading)是一种特性,它允许我们重新定义已有的运算符(如加号、减号、乘号等)的操作行为,使其适用于自定义的类类型或用户定义的数据类型。
通过重载运算符,我们可以使用自定义的语义来执行运算符操作,使得代码更直观、可读性更好,同时也提供了类似内置类型的操作方式。
直白点说就是对原本的运算符赋予其类中的实际意义,运算符的重载是为了让自定义类型可以像内置类型一样去使用运算符。
1.1使用原则
是否要重载一个运算符,看的是这个运算符是否对这个类的对象有意义。
函数名字为:关键字operator后面接需要重载的运算符符号。
函数原型:返回值类型 operator操作符(参数列表)
注意:
(1) 不能通过连接其他符号来创建新的操作符:比如operator@ 。
(2) 重载操作符必须有一个类类型或者枚举类型的操作数。
(3) 用于内置类型的操作符,其含义不能改变,例如:内置的整型+,不能改变其含义。
(4) 作为类成员的函数重载时,其形参看起来比操作数数目少1个成员函数的操作符有一个默认的形参this,限定为第一个形参。
(5) . * :: sizeof ?: 以上5个运算符不能重载。
1.2 示例演示
对于一个日期类,下面我们重载了==
#include<iostream>
using namespace std;
class Date
{
public:
// 构造函数
Date(int year = 0, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
// 拷贝构造
Date(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
void Print()
{
cout << _year << " " << _month << " " << _day << endl;
}
// 赋值重载
Date& operator=(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
return *this;
}
// 这里隐含有this指针,实际调用为bool operator==(Date*this,const Date& d)
bool operator==(const Date& d)const // 加const是保证*this不被修改
{
return _year == d._year && _month == d._month && _day == d._day;
}
private:
int _year;
int _month;
int _day;
};
int main()
{
Date d1(2023, 6, 24);
Date d2(2023, 6, 25);
bool ret = d1 == d2; // 这里实际上是d1.operator=(&d1,d2); 前者更直观
cout << ret << endl; // 0
return 0;
}
下面我们将这个日期类完善一下
二、完善一个日期类
#include<iostream>
using namespace std;
class Date
{
public:
// 构造函数
Date(int year = 0, int month = 1, int day = 1)
:_year(year)
, _month(month)
, _day(day)
{
if (year >= 0
&& month >= 1 && month <= 12
&& day >= 1 && day <= GetMonthDay(year, month))
{
_year = year;
_month = month;
_day = day;
}
else
{
cout << "非法日期" << endl;
}
}
// 拷贝构造函数
Date(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
// d1.operator=(&d1,d2);
Date& operator=(const Date& d)
{
//防止自己给自己赋值
if (this != &d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
return *this;//出了作用域对象还在,使用引用返回
}
//d1.operator<(&d1,d2);
bool operator<(const Date& d)const//bool operator<(Date* 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;
return false;
}
bool operator==(const Date& d)const
{
return _year == d._year && _month == d._month && _day == d._day;
}
// 只要成员函数中不需要直接或间接修改成员变量最好都加上const
// d1.oprerator <= (&d1, d2);
bool operator<=(const Date& d)const // 括号外的const表示不允许修改*this
{
return *this < d || *this == d;//复用上面==的来实现
// 复用的好处是类变动的话只需要改一部分
}
bool operator>(const Date& d)const
{
return !(*this <= d);
}
bool operator>=(const Date& d)const
{
return !(*this < d);
}
bool operator!=(const Date& d)const
{
return !(*this == d);
}
Date operator+(int day)const
{
//复用+=
Date ret(*this);//拷贝构造一个ret
ret += day;
return ret;
}
Date& operator+=(int day) //不能加const,因为要修改*this
{
if (day < 0)
{
return *this -= -_day;
}
_day += day;
while (_day > GetMonthDay(_year, _month))
{
//如果日期的天不合法,就需要往月进位
_day -= GetMonthDay(_year, _month);
++_month;
if (_month == 13)
{
++_year;
_month = 1;
}
}
return *this;
}
// d1 -= 10
Date& operator-=(int day)
{
if (day < 0)
{
return *this += -day;
}
_day -= day;
while (_day <= 0)
{
--_month;
if (_month == 0)
{
--_year;
_month += 12;
}
_day += GetMonthDay(_year, _month);
}
return *this;
}
// d1 - 10
Date operator-(int day)const
{
Date ret = *this;
ret -= day;
return ret;
}
// ++d1 -> d1.operator++(&d1)
Date& operator++() // 默认是前置++
{
*this += 1;
return *this; // 返回自身,*this,出了作用域还在,所以引用作返回值
}
// d1.operator++(&d1,0)
Date operator++(int) //为了构成函数重载,后置++在括号内加了一个int
{
Date tmp(*this);
*this += 1;
return tmp; // 返回加之前的值,而tmp出了作用域后会消失,所以传拷贝
}
// --d1
Date& operator--() // 前置--
{
*this -= 1;
return *this;
}
Date operator--(int) // 后置--
{
Date tmp(*this);
*this -= 1;
return tmp;
}
void Print()
{
cout << _year << "-" << _month << "-" << _day << endl;
}
private:
int GetMonthDay(int year, int month)const
{
static int monthDays[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
if ((month == 2) &&
((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
{
return 29;
}
return monthDays[month];
}
int _year;
int _month;
int _day;
};
int main()
{
Date d1(2023,6,24);
d1.Print();
Date d2(2023, 6, 20);
//d2.Print();
cout << (d1 < d2) << endl;
cout << (d1 > d2) << endl;
cout << (d1 == d2) << endl;
cout << (d1 != d2) << endl;
cout << (d1 <= d2) << endl;
cout << (d1 >= d2) << endl;
Date d3 = d1 + 10;
d3.Print();
return 0;
}