目录
1.赋值运算符重载
1.1 运算符重载
1.2 赋值运算符重载
1.3 日期类实现
1.赋值运算符重载
1.1 运算符重载
①当运算符被用于类类型的对象时,C++语言允许我们通过通过运算符重载的形式指定新的含义。C++规定类类型对象使用运算符时,必须转换成调用对应运算符重载,若没有对应的运算符重载,则会编译报错。
②运算符重载是具有特殊名字的函数,他的名字是由 operator 和后面要定义的运算符共同构成。和其他函数一样,它也具有其返回类型和参数列表以及函数体。
③重载运算符函数的参数个数和该运算符作用的运算对象数量一样多。一元运算符有一个参数,二元运算符有两个参数,二元运算符的左侧运算对象传给第一个参数,右侧运算对象传给第二个参数。
④如果一个重载运算符函数是成员函数,则它的第一个运算对象默认传给隐式的 this 指针,因此运算符重载作为成员函数时,参数比运算对象少一个。
⑤运算符重载以后,其优先级和结合性与对应的内置类型运算符保持一致。
⑥不能通过连接语法中没有的符号来创建新的操作符:比如 operator@。
⑦.* :: sizeof ?: . 注意以上5个运算符不能重载。
⑧重载操作符至少有一个类类型参数,不能通过运算符重载改变内置类型对象的含义,如: int operator+(int x, int y)
⑨一个类需要重载哪些运算符,是看哪些运算符重载后有意义;重载++运算符时,有前置++和后置++,运算符重载函数名都是 operator++,无法很好的区分。C++规定,后置++重载时,增加一个 int 形参,跟前置++构成函数重载,方便区分。
⑩重载 << 和 >> 时,需要重载为全局函数,因为重载为成员函数,this 指针默认抢占了第一个形参位置,第一个形参位置是左侧运算对象,调用时就变成了 对象 <<cout,不符合使用习惯和可读性。重载为全局函数把 ostream/istream 放到第一个形参位置就可以了,第二个形参位置当类类型对象。
Date 比较大小的代码示例:
class Date {
public:Date(int year = 1, int month = 1, int day = 1) {_year = year;_month = month;_day = day;}//private:int _year;int _month;int _day;
};bool operator<(const Date& x1, const Date& x2)
{if (x1._year < x2._year){return true;}else if (x1._year == x2._year && x1._month < x2._month){return true;}else if (x1._year == x2._year&& x1._month == x2._month&& x1._day == x2._day){return true;}return false;
}int main()
{Date d1(2024, 8, 9);Date d2(2024, 8, 10);//bool ret2 = d1 < d2;bool ret2 = operator<(d1, d2);return 0;
}
1.2 赋值运算符重载
赋值运算符重载是一个默认成员函数,用于完成两个已经存在的对象直接的拷贝赋值,
这里要注意跟拷贝构造区分,拷贝构造用于一个对象拷贝初始化给另一个要创建的对象。
如以下的例子:
int main()
{Date d1(2024, 8, 10);Date d2(d1);Date d3(2024, 9, 11);Date d4 = d1;//拷贝构造d1 = d3; //赋值运算符重载return 0;
}
特点:
①赋值运算符重载是一个运算符重载,规定必须重载为成员函数。
赋值运算符重载的参数建议写成 const 当前类类型引用,否则会传值传参会有拷贝。
②有返回值,且建议写成当前类类型引用,引用返回可以提高效率,有返回值是为了支持连续赋值场景。
③没有显式实现时,编译器会自动生成一个默认赋值运算符重载,默认赋值运算符重载行为跟默认拷贝函数类似。(栈需要自己实现深拷贝)
④Date 和 MyQueue 不需要进行赋值运算符重载,Stack 需要进行赋值运算符重载。
(①构造一般需要自己写,自己传参定义初始化;
②析构,构造时有资源申请(如 malloc 或者 fopen)等,就需要显示写析构函数;
③拷贝构造和赋值重载,显示写了析构,内部管理资源,就需要显示实现拷贝构造;)
②的示例如下,这样就支持了连续赋值。
Date& operator = (const Date& d)
{_year = d._year;_month = d._month;_day = d._day;return *this;
}int main()
{d1 = d2 = d3;return 0;
}
为了防止自己给自己赋值,会进行以下的代码修改:
Date& operator = (const Date& d)
{if (this != &d){_year = d._year;_month = d._month;_day = d._day;}return *this;
}
对于③,上述的代码屏蔽之后,我们会发现,代码依旧会正常运行。
1.3 日期类实现
Date.h中的内容:
#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<assert.h>
using namespace std;class Date
{
public:Date(int year = 1900, int month = 1, int day = 1);void Print();int GetMonthDay(int year, int month){assert(month > 0 && month < 13);static int monthDayArray[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;}return monthDayArray[month];}bool operator<(const Date& d);bool operator>(const Date& d);bool operator<=(const Date& d);bool operator>=(const Date& d);bool operator==(const Date& d);bool operator!=(const Date& d);// d1 += 天数Date& operator+=(int day);Date operator+(int day);// d1 -= 天数Date& operator-=(int day);Date operator-(int day);//++d1Date& operator++();//d1++Date operator++(int);//日期减去日期int operator-(const Date& d);
private:int _year;int _month;int _day;
};
Date.cpp中的内容:
#define _CRT_SECURE_NO_WARNINGS 1
#include"Date.h"Date::Date(int year, int month, int day)
{_year = year;_month = month;_day = day;
}void Date::Print()
{cout << _year << "-" << _month << "-" << "_day" << endl;
}bool Date::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;}return false;
}
bool Date::operator>(const Date& d)
{return !(*this <= d);
}
bool Date::operator<=(const Date& d)
{return *this < d || *this == d;
}
bool Date::operator>=(const Date& d)
{return !(*this < d);
}
bool Date::operator==(const Date& d)
{return _year = d._year &&_month == d._month &&_day == d._day;
}
bool Date::operator!=(const Date& d)
{return !(*this == d);
}
//这种不推荐,推荐下面的方法
//Date Date::operator+(int day)
//{
// Date tmp = *this;
// tmp._day += day;
// while (tmp._day > GetMonthDay(tmp._year, tmp._month))
// {
// tmp._day -= GetMonthDay(tmp._year, tmp._month);
// ++tmp._month;
// if (tmp._month == 13)
// {
// tmp._year++;
// tmp._month = 1;
// }
// }
//
// return tmp;
//}
//
//Date& Date::operator+=(int day)
//{
// *this = *this + day;
// return *this;
//}Date& Date::operator+=(int day)
{if (day < 0){return *this -= -day;}_day += day;while (_day > GetMonthDay(_year, _month)){++_month;if (_month == 13){_year++;_month = 1;}}return *this;
}Date Date::operator+(int day)
{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){_month = 12;--_year;}_day += GetMonthDay(_year, _month);}return *this;
}
Date Date::operator-(int day)
{Date tmp(*this);tmp -= day;return tmp;
}//++d1
Date& Date::operator++()
{*this += 1;return *this;
}
//d1++
Date Date::operator++(int)
{Date tmp(*this);*this += 1;return tmp;//后置返回++以前的值
}int Date::operator-(const Date& d)
{Date max = *this;Date min = d;int flag = 1;if (*this < d){max = d;min = *this;flag = -1;}int n = 0;while (min != max){++min;++n;}return n * flag;
}