再再探C++类和对象

1. 赋值运算符重载

对于内置类型我们可以很好的作加减乘除的运算,但是对于自定义类型的运算,编译器不知道如何处理,C++为了代码的可读性引入了运算符重载,运算符重载是具有特殊函数名的函数,也有返回值,参数列表也和普通函数类似。

1.1 运算符重载

函数名:关键字operator后面接要重载的运算符
函数原型:返回值 + operator + 运算符 + 参数。

注:

  1. 不能通过连接其他字符来创造新的操作符,例如:operator#
  2. 重载符的参数列表必须要有一个类类型的参数。
  3. 不能改变运算符的含义,例如重载+,那含义就不能改成其他运算符。
  4. 作为类成员函数重载时,其形参应该比操作数少一个,因为成员函数有一个隐藏的this指针指向一个操作数对象。但是如果是全局函数的话,就需要把操作数参数写全。
  5. “?:”、“.”、“::”、“sizeof”、“.*”。这五个操作符不能进行重载。
class Date
{
public:
	Date(int year = 2023,int month = 10,int day = 26)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	bool operator==(Date& d)
	{
		return _year == d._year && _month == d._month && _day == d._day;
	}
private:
	int _year;
	int _month;
	int _day;
};
int main()
{
	Date d;
	Date d1;
	cout << (d == d1) << endl;
	return 0;
}

在这里插入图片描述

1.2 赋值运算符重载

  1. 格式:
    (1)参数类型为const +(类名)+&
    (2)返回类型为(类名)+ &,如果没有返回值则不能连续赋值
    (3)检查是否自己给自己赋值
    (4)return *this
class Date
{
public:
	Date(int year = 2023,int month = 10,int day = 26)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	
	Date& operator=(Date& d)
	{
		if (this != &d)
		{
			_year = d._year;
			_month = d._month;
			_day = d._day;
		}

		return *this;
	}
	
	void Print()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};
int main()
{
	Date d(1,1,1);
	Date d1 = d;
	d1.Print();
	return 0;
}

在这里插入图片描述

  1. 和运算符重载不同的是,赋值运算符重载必须作为类内部函数,因为赋值运算符重载是默认成员函数,你不在内类显示定义,编译器会自动生成,会产生冲突。
    在这里插入图片描述

  2. 没有显示定义该函数时,编译器默认执行的是浅拷贝。和拷贝构造类似,对于内置类型直接按字节拷贝,对于自定义类型,调用它的赋值重载运算符。

  3. 该函数的功能和拷贝构造函数非常的相似,但不同的是,拷贝构造函数是,一个已经存在的对象拷贝出另一个对象,赋值重载运算符,是对量个已经存在的对象操作。

1.3 前置++和后置++的重载

class Date
{
public:
	Date(int year = 2023,int month = 10,int day = 26)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	//前置++,返回的是+1后的结果,所以直接返回本身
	Date& operator++()
	{
		_day++;
		return *this;
	}
	//后置++,返回的是+1前的结果
	//C++规定:后置++重载时多增加一个int类型的参数,但调用函数时该参数不用传递,编译器自动传递
	//tmp出了作用域会销毁,所以不能传引用
	Date operator++(int)
	{
		Date tmp(*this);
		_day++;
		return tmp;
	}

	void Print()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};

int main()
{
	Date d(1,1,1);
	d++;
	++d;
	d.Print();
	return 0;
}

在这里插入图片描述

2. const成员

大家应该对const这个关键字不陌生了,接下来我们来看一段代码

class Date
{
public:
	Date(int year,int month,int day)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	
	void Print()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};
int main()
{
	const Date d1(2023,11,2);
	d1.Print();
	return 0;
}

这段代码有什么问题呢?

这的问题其实和我们之前博客将的常引用的问题是一样的:权限放大了,我们知道成员函数有一个隐藏的this指针指向对象,该指针的类型是Date* ,但在主函数,我的对象指针的类型为const Dtae*,造成了权限的放大,那么如何解决这个问题呢?
在这里插入图片描述

因为this指针不能显示的写,所以我们用const修饰该成员函数,那么该成员函数称为const成员函数但其实我们知道,这个const修饰的是隐藏的this指针。接下来我们看一下修改后的代码。

class Date
{
public:
	Date(int year,int month,int day)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	//将成员函数变成const成员函数
	void Print() const
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};
int main()
{
	const Date d1(2023,11,2);
	d1.Print();
	return 0;
}

在这里插入图片描述

3. 取地址符重载和const取地址符操作

这两个也是默认构造函数,不显示的定义,编译器会自动生成,但这个也没有显示的写的必要,所以一般都不会写。

class Date
{
public:
	Date* operator&()
	{
		return this;
	}
	const Date* operator&()const
	{
		return this;
	}
private:
	int _year; // 年
	int _month; // 月
	int _day; // 日
};

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值