运算符重载
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.自动调用
本章用于记笔记,如果有不对或者不足的地方,欢迎大佬们指正,补充。感谢大家的阅读,如果感觉博主写的还可以,麻烦点个赞支持一下,阿里嘎多。