赋值运算符重载
//类内默认有赋值运算符, 浅拷贝
//参数类型:
// 值: 传参发生拷贝, 且不能优化自赋值的逻辑
// 引用:不发生拷贝, 可以优化自赋值的逻辑
// 返回值类型:
// void: 不能进行连续赋值
// 引用/值: 可以进行连续赋值, 如果是引用,效率更高
// return *this;
class Date{
public:
Date(int y = 2000, int m = 1, int d = 1)
:_year(y), _month(m), _day(d)
{}
Date& operator=(const Date& d)
{
//检查是否给自己赋值
if (this != &d)
{
_year = d._year;
_month = d._month;
_day = d._day;
cout << "operator=" << endl;
}
return *this;
}
private:
int _year;
int _month;
int _day;
}
int main()
{
Date d1(2008, 1, 1);
Date d2, d3, d4;
// 赋值运算符重载返回引用可进行连续赋值
d4 = d3 = d2 = d1;
return 0;
}
其他运算符
- 加号运算符
class A
{
public:
A(int a = 0)
{
_a = a;
_b = 0;
}
// 运算符重载函数为成员函数是,需要显示定义的参数个数比操作符要求的个数少一个
// 定义为全局函数的时候, 传两个参数
// 编译器会自动加入一个参数: this, 其占据参数列表的第一个位置
A operator+(A& a1) // A operator(A* this, A& a1)
{
//return A(a1._a + a1._b + a2._a + a2._b);
int a = this->_a + this->_b + a1._a + a1._b;
A aobj(a);
return aobj;
}
int _a;
int _b;
};
/*
A operator+(A& a1, A& a2){
return A(a1._a + a1._b + a2._a + a2._b);
}
*/
#include <iostream>
using namespace std;
namespace date1
{
class Date
{
public:
Date(int year = 1, int month = 1, int day = 1)
{
//判断参数是否有效
if (year <= 0
|| month <= 0 || month > 12
|| day > getMonthDay(year, month))
{
cout << "非法日期!" << endl;
_year = _month = _day = 1;
cout << "日期重置为:1-1-1" << endl;
}
else
{
_year = year;
_month = month;
_day = day;
}
}
/*
Date(const Date& date);
Date& operator=(const Date& date);
~Date();
*/
// 2020-5-1 + 10 ----> 2020-5-21
// 5-101 --> - 31 --> 6-70 --> -30 --> 7-40 --> -31 --> 8-9
// d.operator+=(day) d += day
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)
{
++_year;
_month = 1;
}
}
return *this;
}
//2020-5-1 - 100
//2020-5-(-99) --> +30 --> 4-(-69) --> + 31 --> 3-(-38) --> + 29 --> 2-(-9) --> + 31 --> 1-22
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;
}
int getMonthDay(int year, int month)
{
static int days[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int day = days[month];
//二月特殊处理
if (month == 2
&& ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
++day;
return day;
}
//前置自加 ++date --> 首先自加,再返回自加之后的值
Date& operator++() // Date& operator++(Date* this) ++d d.operator++()
{
return *this += 1;
}
//后置自加 date++ --> 返回自加之前的值,再进行自加运算
Date operator++(int)
{
Date ret(*this); //拷贝构造
*this += 1; // 等效 (*this).operator+=(1);
return ret;
}
//前置自减
Date& operator--()
{
return *this -= 1;
}
//后置自减
Date operator--(int)
{
Date ret(*this);
*this -= 1;
return ret;
}
// date + day int a = 10, int b = 2; a - b
Date operator+(int day)
{
Date ret(*this);
return ret += day;
}
// date - day
Date operator-(int day)
{
Date ret(*this);
return ret -= day;
}
//比较
bool operator<(const Date& date)
{
if (_year < date._year)
return true;
else if (_year == date._year)
{
if (_month < date._month)
return true;
else if (_month == date._month)
{
if (_day < date._day)
return true;
}
}
return false;
}
bool operator>(const Date& date)
{
return !(*this <= date);
}
bool operator==(const Date& date)
{
return this->_year == date._year
&& this->_month == date._month
&& this->_day == date._day;
}
bool operator>=(const Date& date)
{
return *this > date || *this == date;
}
bool operator<=(const Date& date)
{
return *this < date || *this == date;
}
bool operator!=(const Date& date)
{
return !(*this == date);
}
// eg: 2020.5.13 - 2019.4.15
int operator-(Date& date)
{
if (*this > date)
{
int day = 0;
Date ret = date;
while (ret != *this)
{
++ret;
++day;
}
return day;
}
else
{
Date ret = date;
int day = 0;
while (ret != *this)
{
--ret;
--day;
}
return day;
}
}
void print()
{
cout << _year << "-" << _month << "-" << _day << endl;
}
private:
int _year;
int _month;
int _day;
};
}
/*
Date& operator++(Date& date)
{
return date += 1;
}
Date operator++(Date& date, int)
{
Date ret = date;
date += 1;
return ret;
}
*/
不能重载的运算符
1.作用域操作符 ::
2.条件操作符 ? :
3.点操作符 .
4.指向成员操作的指向操作符 ->* , .*
5.预处理符号 #, sizeof