mall :hutool项目源码解析 作者:忆遂愿
https://cloud.tencent.com/developer/article/2466055?shareByChannel=link
文章通过实际项目需求引导对 Hutool 的学习,在工具类介绍环节,依据不同工具类在控制层接口的应用以及控制台打印信息进行讲解,同时紧密关联官方开发文档,为读者自主深入研究提供了清晰的指引路径,非常有帮助。
C++中预定义的运算符的操作对象只能是基本数据类型。但实际上,对于许多用户自定义类型(例如类),也需要类似的运算操作。这时就必须在C++中重新定义这些运算符,赋予已有运算符新的功能,使它能够用于特定类型执行特定的操作。
C++为了增强代码的可读性引入了运算符重载,运算符重载是具有特殊函数名的函数,也具有其
返回值类型,函数名字以及参数列表,其返回值类型与参数列表与普通的函数类似
运算符重载是通过创建运算符函数实现的,运算符函数定义了重载的运算符将要进行的操作。
操作符重载,本质上就是函数重载,它大大丰富了已有操作符的含义,方便使用
运算符重载格式如下:

1.函数名:operator+需要重载的运算符符号 2.函数原型:返回值类型 operator+符号(形参参数列表) 3.必须有一个类类型的参数 4. :: ?: . .* sizeof 这五个运算符不能重载 5.用于内置类型的运算符,其含义不能改变,例如:内置的整型+,不能改变其含义 6.作为类成员函数重载时,其形参看起来比操作数数目少1,因为成员函数的第一个参数为隐 藏的this 7.不能通过连接其他符号来创建新的操作符:比如operator@ 8.运算符重载时必须遵循的原则重载运算符含义必须清楚; 重载运算符不能有二义性。 9.算符函数重载的两种形式 重载为类的成员函数 重载为类的非成员函数 (非成员函数通常是友元函数)。
注:可以把一个运算符作为一个非成员、非友元函数重载。但是,这样的运算符函数访问类的私有和保护成员时,必须使用类的公有接口中提供的设置数据和读取数据的函数,调用这些函数时会降低性能。可以内联这些函数以提高性能。
代码
#include<iostream>
using namespace std;
//负数类
class complex
{
public:
complex(double r = 0, double i = 0) :_real(r), _imag(i) {}
complex operator +(const complex& c); //+运算符
complex operator -(const complex& c);//-运算符
complex& operator +=(const complex& c); //+=运算符
complex& operator -=(const complex& c);//-=运算符
complex& operator - ();//求负运算符
void Print()const
{
cout << "(" << _real << "," << _imag << ")" << endl;
}
private:
double _real;//实部
double _imag;//虚部
};
complex complex::operator +(const complex& c) //+运算符
{
complex tmp;
tmp._real = _real + c._real;
tmp._imag = _imag + c._imag;
return tmp;
}
complex complex::operator -(const complex& c) //-运算符
{
complex tmp;
tmp._real = _real - c._real;
tmp._imag = _imag - c._imag;
return tmp;
}
complex& complex::operator +=(const complex& c) //+=运算符
{
_real += c._real;
_imag += c._imag;
return *this;
}
complex& complex::operator -=(const complex& c) //-=运算符
{
_real -= c._real;
_imag -= c._imag;
return *this;
}
complex& complex::operator - ()//求负运算符
{
_real = -_real;
_imag = -_imag;
return *this;
}
int main()
{
complex c1(3.5, 5), c2(6, 8), c3, c4, c5;
c3 = c1 + c2;
c3.Print();
c4 = c2 - c1;
c4.Print();
c1 += c2;
c1.Print();
c2.Print();
c2 -= c1;
c1.Print();
c2.Print();
c5 = -c2;
c5.Print();
return 0;
}
前置++和后置++的函数名都是operator++(没错,又是函数重载),他们的区别在于前置++没有形参,后置++有一个形参int,但是我们在实际上使用时并不需要给后置++的形参int传实参,int只是为了区分前置++和后置++的标识。
前置--和后置--也是同样用形参int来区分。
#include<iostream>
using namespace std;
class Date
{
public:
Date(int year = 0, int month = 0, int day = 0);
// 拷贝构造函数
// d2(d1)
Date(const Date& d);
// 获取某年某月的天数
int GetMonthDay(int year, int month);
// 日期+=天数
Date& operator+=(int day);
// 日期+天数
Date operator+(int day)const;
// 日期-=天数
Date& operator-=(int day);
// 日期-天数
Date operator-(int day)const;
// 前置++
Date& operator++();
// 后置++
Date operator++(int);
// 后置--
Date operator--(int);
// 前置--
Date& operator--();
void print()const;
// 析构函数(日期类无需清理资源,析构函数不必显示写)
//void print();
//~Date()
//{
//cout << "~Date()" << endl;
//}
private:
int _year, _month, _day;
};
Date::Date(int year, int month, int day)
{
_year = year;
_month = month;
_day = day;
}
Date::Date(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
int Date::GetMonthDay(int year, int month)
{
static int MonthDay[13] = { -1,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;
}
else
{
return MonthDay[month];
}
}
void Date::print()const
{
cout << _year << "年" << _month << "月" << _day << "日" << endl;
}
Date& Date::operator+=(int day)
{
if (day < 0)
{
return *this -= -day;
}
_day += day;
while (_day > GetMonthDay(_year, _month))
{
_day -= GetMonthDay(_year, _month);
_month++;
if (_month == 13)
{
_month = 1;
_year++;
}
}
return *this;
}
Date Date::operator+(int day)const
{
Date tmp = *this;
tmp += day;
return tmp;
}
Date& 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;
}
Date Date::operator-(int day)const
{
Date tmp = *this;
tmp -= day;
return tmp;
}
void Test1()
{
Date d1(2024, 4, 30);
Date d2 = d1 + 3;
d2.print();
Date d3(2024, 12, 31);
Date d5 = d3;
d3 += 1;
d3.print();
d5 = d5 - 1;
d5.print();
d1 -= 30;
d1.print();
Date d4 = d1 - 3;
d4.print();
}
// 前置++
Date& Date::operator++()
{
//这里直接用刚刚实现的Date& Date::operator+=(int day)
//只是++相当于day=1而已
//减少了代码负担
*this += 1;
return *this;
}
// 后置++
Date Date::operator++(int)
{
//这里直接用刚刚实现的Date& Date::operator+(int day)
Date tmp = *this;
*this += 1;
return tmp;
}
// 前置--
Date& Date::operator--()
{
*this -= 1;
return *this;
}
// 后置--
Date Date::operator--(int)
{
Date tmp = *this;
*this -= 1;
return tmp;
}
void Test2()
{
Date d1(2024, 6, 1);
Date d2(2023, 12, 31);
Date d3 = d1--;
Date d4 = d2++;
d3.print();
d4.print();
Date d5 = --d3;
Date d6 = ++d4;
d5.print();
d6.print();
}
int main()
{
//Test1();//可以自行测试
Test2();
}
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。