个人主页:Lei宝啊
愿所有美好如期而遇
以下两个默认成员函数一般不用重新定义 ,编译器默认会生成。
#include <iostream>
using namespace std;
class Date
{
public:
	Date()
		:_year(2023)
		,_month(10)
		,_day(28)
	{}
	Date* operator&()
	{
		return this;
	}
	const Date* operator&() const
	{
		return this;
	}
private:
	int _year;
	int _month;
	int _day;
};
int main()
{
	Date a;
	cout << &a << endl;
	const Date b;
	cout << &b << endl;
	return 0;
}
这两个运算符一般不需要重载,使用编译器生成的默认取地址的重载即可,只有特殊情况,才需
要重载,比如 想让别人获取到指定的内容!
#include <iostream>
using namespace std;
class Date
{
public:
	Date()
		:_year(2023)
		,_month(10)
		,_day(28)
	{}
	Date* operator&()
	{
		return nullptr;
	}
	const Date* operator&() const
	{
		return this;
	}
private:
	int _year;
	int _month;
	int _day;
};
int main()
{
	Date a;
	cout << &a << endl;
	const Date b;
	cout << &b << endl;
	return 0;
}
甚至我们可以返回一个错误的地址(滑稽)