文章目录
- 1.赋值运算符重载
- 1.1运算符重载
- 1.2 赋值运算符重载
- 1.3 前置++和后置++重载
 
- 2.日期类的实现
- 3. const成员函数
- 4 取地址及const取地址操作符重载
 
 
 
上文介绍了前三个默认成员函数,本文会介绍剩下三个,
赋值重载会重点展开。 
 
1.赋值运算符重载
1.1运算符重载
C++为了增强代码的可读性引入了运算符重载,运算符重载是具有特殊函数名的函数,也具有其
 返回值类型,函数名字以及参数列表,其返回值类型与参数列表与普通的函数类似。
函数名字为:关键字operator后面接需要重载的运算符符号。
 函数原型:返回值类型operator操作符(参数列表)
我们知道,类是不能用符号直接进行比较的,但如果要用 运算符 > 来比较类A是否大于类B,此时就可以重载运算符>:(Date类举例)
class Date
{
public:
	Date(int year = 1900, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	bool operator>(Date& tmp)
	{
		return this->_year > tmp._year;
	}
private:
	int _year;
	int _month;
	int _day;
};
int main()
{
	Date d1(2024, 2, 2);
	Date d2(2021, 2, 2);
	//使用重载后d1和d2就可以按照自己设定的规则比较
	cout << (d1 > d2) << endl;
}
注意:
- 不能通过连接其他符号来创建新的操作符:比如operator@(本来@就不是运算符,不能重载)
- 重载操作符必须有一个类的类型参数。
- 不能重载内置类型的运算符例如:内置的整型+,不 能改变其含义。
- 作为类成员函数重载时,其形参看起来比操作数数目少1,因为成员函数的第一个参数为隐藏的this(下面的代码Date类做个解释):
class Date
{
public:
	Date(int year = 1900, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	//看似只有一个参数,其实隐藏了一个固定的参数this,并且是固定的第一个参数
	bool operator>(Date& tmp)
	{
		return this->_year > tmp._year;
	}
private:
	int _year;
	int _month;
	int _day;
};
- 运算符重载后,可以显示调用,也可以转换调用,其反汇编指令完全相同:
//显示调用
d1 > d2;
//转换调用
d1.operator>(d2);

- .* :: sizeof ?: . 注意以上5个运算符不能重载。
 PS:- .*用法:
class Date
{
public:
	//成员函数 f
	void fun()
	{
	}
private:
	int _year;
	int _month;
	int _day;
};
typedef void(Date::* Ptr)();//1.将 函数指针类型void(*)() 重命名为 Ptr ,并且指定在Date类内
int main()
{
	Ptr ptr = &Date::fun; // 2.void(*)()类型的 ptr 指向Date类内的成员函数
	Date test;//3.实例化test
	(test.*ptr)();//4. 通过ptr调用test的fun函数,就要用到.*
}
1.2 赋值运算符重载
class Date
{
public:
	Date(int year = 1900, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	Date(const Date& d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}
	//赋值运算符重载
	Date& operator=(const Date& d)
	{
		if (this != &d)
		{
			_year = d._year;
			_month = d._month;
			_day = d._day;
		}
		return *this;
	}
private:
	int _year;
	int _month;
	int _day;
};
- 赋值运算符重载格式
- 参数类型:const T(类型)& ->传递引用可以提高传参效率
- 返回值类型:T(类型)& -> 返回引用可以提高返回的效率,有返回值目的是为了支持连续赋值
- 检测是否自己给自己赋值
- 返回*this:要复合连续赋值的含义
Date& operator=(const Date& d)
	{
		if (this != &d)
		{
			_year = d._year;
			_month = d._month;
			_day = d._day;
		}
		return *this;
	}
-  赋值运算符只能重载成类的成员函数不能重载成全局函数,否则会编译错误。 
  
 为什么呢?
 原因:赋值运算符如果不显式实现,编译器会生成一个默认的。此时用户再在类外自己实现一个全局的赋值运算符重载,就和编译器在类中生成的默认赋值运算符重载冲突了,故赋值运算符重载只能是类的成员函数。
-  在类中,用户没有显式实现时,编译器会生成一个默认赋值运算符重载,以值的方式逐字节拷贝(类似默认拷贝构造)。 对于内置类型成员变量,是直接赋值的,对于自定义类型成员变量,调用对应类的赋值运算符重载完成赋值。
  
 注意:如果类中未涉及到资源管理,赋值运算符是否实现都可以;一旦涉及到资源管理则必须要实现。因为默认的赋值重载实质上也是浅拷贝。
1.3 前置++和后置++重载
我们知道,无论是前置++还是后置++,其所用的运算符都是 ++。如果我们要对其重载,要如何分别实现前置++和后置++的重载呢?
C++规定:后置++重载时强制增加一个int类型的参数,但调用函数时该参数不用传递,编译器自动传递
这里也用Date类来举例:
//前置++重载:
//返回*this,也就是该类本身
Date& operator++()
{
	_day += 1;
	return *this;
}
//后置++重载:为了区分,强制加了一个int类型做区分
//后置++返回的是+1之前的值,所以要拷贝一个临时类来返回,而类本身要+1
//而temp是临时对象,因此只能以值的方式返回,不能返回引用
Date operator++(int)
{
	Date temp(*this);
	_day += 1;
	return temp;
}
此后所有的前置或后置类型符号的重载,都形似前置++和后置++的方式区分
2.日期类的实现
日期类的实现不仅是完整的实现其成员变量和成员函数,也要全套设计其运算符重载。
//Date.h
#pragma once
#include <iostream>
using namespace std;
class Date
{
public:
	//打印
	void Print()
	{
		cout << _year << "." << _month << "." << _day<<endl;
	}
	// 获取某年某月的天数
	//因为该成员函数要频繁调用,所以就放在类内
	int GetMonthDay(int year, int month)
	{
		
		static int a[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30,31 };
		int day = a[month];
		if (month == 2 && (year % 4 == 0 && year % 100 != 0 || year % 400 == 0))
		{
			day = 29;
		}
		return day;
	}
	// 全缺省的构造函数
	Date(int year = 1900, int month = 1, int day = 1);
	// 拷贝构造函数
	// d2(d1)
	Date(const Date& d);
	// 赋值运算符重载
	// d2 = d3 -> d2.operator=(&d2, d3)
	Date& operator=(const Date& d);
	// 析构函数
	~Date();
	// 日期+=天数
	Date& operator+=(int day);
	// 日期+天数
	Date operator+(int day);
	// 日期-天数
	Date operator-(int day);
	// 日期-=天数
	Date& operator-=(int day);
	// 前置++
	Date& operator++();
	// 后置++
	Date operator++(int);
	// 后置--
	Date operator--(int);
	// 前置--
	Date& operator--();
	// >运算符重载
	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);
	// 日期-日期 返回天数
	int operator-(const Date& d);
private:
	int _year;
	int _month;
	int _day;
};
//Date.c
#include "Date.h"
//构造
Date::Date(int year, int month, int day )
{
	this->_year = year;
	this->_month = month;
	this->_day = day;
}
//拷贝构造
Date::Date(const Date& d)
{
	this->_year = d._year;
	this->_month = d._month;
	this->_day = d._day;
}
//赋值拷贝
Date& Date::operator=(const Date& d)
{
	this->_year = d._year;
	this->_month = d._month;
	this->_day = d._day;
	return *this;
}
//析构函数
Date::~Date()
{
	_year = 0;
	_month = 0;
	_day = 0;
}
// +=重构
Date& Date::operator+=(int day)
{
	_day += day;
	while(_day > GetMonthDay(_year, _month))
	{
		_day -= GetMonthDay(_year, _month);//month = 12
		_month++;// month = 13
		if (_month > 12)
		{
			_month = 1;
			_year++;
		}
	}
	return *this;
}
// +重构
Date Date::operator+(int day)
{
	Date tmp = *this;
	tmp += day;
	return tmp;
}
// -=重构
Date& Date::operator-=(int day)
{
	_day -= day;
	while (_day <= 0)
	{
		 _month -= 1;
		if (_month == 0)
		{
			_month = 12;
			_year--;
		}
		_day += GetMonthDay(_year, _month);
	}
	return *this;
}
// -重构
Date Date::operator-(int day)
{
	Date tmp = *this;
	tmp -= day;
	return tmp;
}
// 前置++重构
Date& Date::operator++()
{
	*this = *this + 1;
	return *this;
}
// 后置++重构
Date Date::operator++(int)
{
	Date tmp = *this;
	*this = *this + 1;
	return tmp;
}
// 前置--重构
Date& Date::operator--()
{
	*this = *this - 1;
	return *this;
}
// 后置--重构
Date Date::operator--(int)
{
	Date tmp = *this;
	*this = *this - 1;
	return tmp;
}
// ==重构
bool Date::operator==(const Date& d)
{
	return d._year == _year && d._month == _month && d._day == _day;
}
// >重构
bool Date::operator>(const Date& d)
{
	if (_year > d._year)
	{
		return true;
	}
	else if (_year == d._year && _month > d._month)
	{
		return true;
	}
	else if (_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)
{
	if (_year < d._year)
	{
		return true;
	}
	else if (_year == d._year && _month < d._month)
	{
		return true;
	}
	else if (_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);
}
// 日期-日期 返回天数
int Date::operator-(const Date& d)
{
	Date max = *this;
	Date min = d;
	int flag = -1;
	if (max < min)
	{
		max = d;
		min = *this;
		flag = 1;
	}
	int n = 0;
	while (max != min)
	{
		max--;
		n++;
	}
	return n * flag;
}
3. const成员函数
将const修饰的“成员函数”称之为const成员函数,const修饰类成员函数,实际修饰该成员函数隐含的this指针,表明在该成员函数中不能对类的任何成员进行修改。
class Date
{
public:
	//const修饰成员函数
	//    该放在这里↓ 
	void fun() const
	{
	}
private:
	int _year;
	int _month;
	int _day;
	Time test1;
};

4 取地址及const取地址操作符重载
这两个默认成员函数一般不用重新定义 ,编译器默认会生成。
class Date
{
public:
	//返回this指针也就是是Date类的地址
	Date* operator&()
	{
		return this;
	}
	//为了防止权限的放大,也会重载一个返回值是const修饰的指针
	const Date* operator&()const
	{
		return this;
	}
private:
	int _year; // 年
	int _month; // 月
	int _day; // 日
};



















