操作符重载
我们用类的时候总要使用-,+,=,*,/,++,--,==,!=等操作符,还是以日期类为例.
当然日期类就一般不会使用*,/了=.=
早操作符重载的格式如下
//返回值类型+operator+要重载的符号+(形参) Date& operator=(Date& d1 ,const Date& d2); //上方就是我对=号赋值符的一次实现.
而具体的我们要看实现.
比如我们可以选择在类里实现或是在类外实现.
先来一个日期类吧
class date { public: void Init(int year = 1, int month = 1, int day = 1) { _year = year; _month = month; _day = day; } void Print() { cout << _year << "-" << _month << "-" << _day << endl; } private: int _day; int _month; int _year; };
我们先用类外的方式实现一个==看看吧
bool operator==(const Date& d1, const Date& d2) { if (d1._year == d2._year && d1._month == d2._month && d1._day == d2._day) { return true; } else { return false; } }
这个其实并不能使用因为我们_year是私有形势存储的,要想使用就必须将_day,_month,_year公有化或者使用内部共有函数来得到他们的值,因为我们只是演示,所以我暂时先把他公有化出来.
我们实现好了这个==运算符后
上图框起的两种方式都可以使用到我们的==重载功能,但是我们偏向使用下面的a==b.
在我们使用a == b的时候其实编译器会帮我们换成第一种的形式—operator==(a,b).
不过我们还是把这些运算符放在类里较好,及保证了类的封装性,又保证了我们元素不被外界访问.
我们将操作符重载放在类里需要对格式进行稍微改变.
// ==运算符重载 bool operator==(const Date& d) { if (_year == d._year && _month == d._month && _day == d._day) { return true; } else { return false; } }
在类内定义的格式其实和外面定义的几乎没啥区别,除了少了一个变量,在使用_year的时候我们不用把它变成共有化的了.
其实少的那个变量使用了this指针来代替.而_year其实也是通过this指针得到的.
但是我们的this是两个变量的那个呢?
来看看我们如何使用类里定义的重构函数就知道了.
我们在使用的时候的形势其实注定了,上面的①中的this其实就是a的地址.
而下面的②其实this也是a的地址.
两者在编译看来没有区别.编译器也会2变成1后,然后编译.
如果有两个变量一般是左边的左this指针,三个变量就是最左的是this指针
类比思考一下,我们的前置后置++ --等其实在类内实现的时候就没必要专门设置形参只需要一个this指针就够了.但是这样就没办法分别了,所以我们规定后置类型要创建一个int类型的形参用于函数分辨.以便形成重构.
下面是对日期类所有操作符重载的实现,我们在实现的时候,有些部分可以操作符可以复用最好复用,复用带来的好处有很多,不仅方便,而且后期找bug也可以减少低级错误.
class Date { public: void Print() { cout << _year << "_" << _month << "_" << _day << endl; } // 获取某年某月的天数 int GetMonthDay(int year, int month) { if (month > 12) { cout << "月输入错误" << endl; return -1; } int const arr[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };//保存每个月的日期 if (month == 2 && JudgeLeapYear(year))//闰年 { return 29; } return arr[month]; } // 全缺省的构造函数 Date(int year = 1900, int month = 1, int day = 1) { _year = year; _month = month; _day = day; } // 拷贝构造函数 // d2(d1) Date(const Date& d) { _year = d._year; _month = d._month; _day = d._day; } // 赋值运算符重载 // d2 = d3 -> d2.operator=(&d2, d3) Date& operator=(const Date& d) { _year = d._year; _month = d._month; _day = d._day; return *this; } // 析构函数 ~Date() { ;//不写用默认的也可 } // 日期+=天数 Date& operator+=(int day) { *this = *this + day;//复用+ return *this; } // 日期+天数 Date operator+(int day) { Date tmp(*this); (tmp._day) += day; while (tmp._day >= GetMonthDay(tmp._year, tmp._month))//判断我们的day是不能超过当月的最大数值的. { tmp._day -= GetMonthDay(tmp._year, tmp._month); tmp._month += 1; if (tmp._month == 13) { tmp._month -= 12; tmp._year += 1; } } return tmp; } // 日期-天数 Date operator-(int day) { Date tmp(*this); (tmp._day) -= day; while (tmp._day <= 0) { tmp._day += GetMonthDay(tmp._year, tmp._month); tmp._month -= 1; if (tmp._month == 0) { tmp._month += 12; tmp._year -= 1; } } return tmp; } // 日期-=天数 Date& operator-=(int day) { _day -= day; while (_day <= 0) { _day += GetMonthDay(_year, _month); _month -= 1; if (_month == 0) { _month = 12; _year -= 1; } } return *this; } // 前置++ Date& operator++() { *this += 1; return *this; } // 后置++ Date operator++(int)//传的int是语法规定 { Date tmp(*this); *this += 1; return tmp; } // 后置-- Date operator--(int) { Date tmp(*this); *this -= 1; return tmp; } // 前置-- Date& operator--() { *this -= 1; return *this; } // >运算符重载 bool operator>(const Date& d) { if (_year > d._year || (_year == d._year && _month > d._month) || (_year == d._year && _month == d._month && _day > d._day)) { return true; } else { return false; } } // ==运算符重载 bool operator==(const Date& d) { if (_year == d._year && _month == d._month && _day == d._day) { return true; } else { return false; } } //>=运算符重载 inline bool operator >= (const Date& d) { if (*this > d || *this == d) { return true; } else { return false; } } // <运算符重载 bool operator < (const Date& d) { return !(*this >= d); } // <=运算符重载 bool operator <= (const Date& d) { if (*this < d || *this == d) { return true; } else { return false; } } // !=运算符重载 bool operator != (const Date& d) { return !(*this == d); } // 日期-日期 返回天数 int operator-(const Date& d) { Date min = (*this > d ? d : *this); Date max = (*this > d ? *this : d); int ret = 0; while (min != max) { min++; ret++; } return ret; } private: bool JudgeLeapYear(int year) { if ((year % 4 == 0 && year % 100 != 0) || year % 100 == 0) { return true; } else { return false; } } private: int _year; int _month; int _day; };
还有&的重载但是这个一般使用编译器自动生成的就好.格式和前面的一样.
class Date { public : Date* operator&() { return this ; } const Date* operator&()const { return this ; } private : int _year ; // 年 int _month ; // 月 int _day ; // 日 };
值得提一嘴的是,我们const修饰指针的时候也是可以组成函数重载的,被const修饰的指针在C++的命名规则下会加上一个K.