Let’s Make C++ Great Again——运算符重载浅尝

本文介绍了C++中的运算符重载,强调了重载时应注意的规则,如不能改变内置类型运算符的含义,并通过实例展示了如何在类中实现重载`==`运算符以保持封装性。此外,还探讨了赋值运算符重载的重要性,包括自我赋值检测、返回*this以及默认赋值构造函数的潜在问题。最后,提醒在涉及指针成员时必须自定义赋值运算符以避免浅拷贝问题。
摘要由CSDN通过智能技术生成

运算符重载浅尝

C++为了增强代码的可读性引入了运算符重载,运算符重载是具有特殊函数名的函数,也具有其返回值类型,函数名字以及参数列表,其返回值类型与参数列表与普通的函数类似。

函数名字为:关键字operator后面接需要重载的运算符符号

函数原型:返回值类型 operator操作符(参数列表)

注意:

  1. 不能通过连接其他符号来创建新的操作符:比如operator@
  2. 重载操作符必须有一个类类型或者枚举类型的操作数
  3. 用于内置类型的操作符,其含义不能改变,例如:内置的整型+,不 能改变其含义
  4. 作为类成员的重载函数时,其形参看起来比操作数数目少1成员函数的
  5. 操作符有一个默认的形参this,限定为第一个形参
  6. .* 、:: 、sizeof 、?: 、. 注意以上5个运算符不能重载。这个经常在笔试选择题中出现。
    ps:.*恐怕很难见到。
class Date
{
public:
	Date(int _year = 0, int _month = 0, int _day = 0)
	{
		this->_year = _year;
		this->_month = _month;
		this->_day = _day;
	}

	int _year;
	int _month;
	int _day;
};
// (d1 == d2) 会被当作 operator==(d1, d2)
// 也自然需要两个参数
bool operator==(const Date& former, const Date& later)
{
	return former._year == later._year
		&& former._month == later._month
		&& former._day == later._day;
}
int main()
{
	Date d1(2022, 1, 1);
	Date d2(2021, 1, 1);

	cout << (d1 == d2) << endl; // 0 比较表达式的括号必须加上,因为流插入符的优先级大于比较运算符。
	return 0;
}

我们将==运算符重载函数写成了全局函数,其中,我们不得不将Date类中的成员变量的权限变为public,从而难以保持我们类的封装性。

有两个方法可以解决这个办法。

  1. 友元函数,相当于允许这个函数对类中所有权限的对象进行访问。但是这个方法还是有些治标不治本。只不过把口子开的小了一些而已。
  2. 将这个函数写到类中。这个方法更常用。
    class Date
    {
    public:
    	Date(int _year = 0, int _month = 0, int _day = 0)
    	{
    		this->_year = _year;
    		this->_month = _month;
    		this->_day = _day;
    	}
    	bool operator==(const Date& later)
    	{
    		return this->_year == later._year
    			&& this->_month == later._month
    			&& this->_day == later._day;
    	}
    private:
    	int _year;
    	int _month;
    	int _day;
    };
    
    int main()
    {
    	Date d1(2022, 1, 1);
    	Date d2(2021, 1, 1);
    
    	cout << (d1 == d2) << endl;
    	return 0;
    }
    
    与写成全局变量不同的地方就是只有一个参数,实质上还是两个参数(this指针):bool operator==(const Date& later)—>bool operator==(Date* const this, const Date& later)

赋值运算符重载

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;
};
int main()
{
	Date d1(2022, 1, 1);
	Date d2 = d1; // 这步操作调用的是拷贝构造函数
	Date d3;
	d3 = d1; // 调用赋值构造函数 相当于:d3.operator=(d1)
	d1 = d2 = d3; // 返回值允许链式赋值
	return 0;
}

赋值运算符重载需要注意的五点:

  1. 参数类型
  2. 返回值
  3. 检测是否自己给自己赋值
  4. 返回*this
  5. 一个类如果没有显式定义赋值运算符重载,编译器也会生成一个,完成对象按字节序的值拷贝。
    class Date
    {
    public:
    	Date(int year = 1900, int month = 1, int day = 1)
    	{
    		_year = year;
    		_month = month;
    		_day = day;
    	}
    private:
    	int _year;
    	int _month;
    	int _day;
    };
    int main()
    {
    	Date d1(2022, 1, 1);
    	Date d2;
    	d2 = d1; // 使用默认生成的赋值构造函数
    	return 0;
    }
    

默认生成的赋值构造函数处理方式与默认生成的拷贝构造函数是相似的。

当成员变量不扯到指针时,可以使用编译器默认生成的;而一旦成员变量存在指针类型,必须自己来实现,防止浅拷贝造成的问题。
在这里插入图片描述
在这里插入图片描述
小小总结:

  1. 构造函数和析构函数处理机制基本相似。
  2. 拷贝构造和赋值构造处理机制基本相似。
  3. 在这里插入图片描述
    如果类中存在属性在堆区,做拷贝构造和赋值构造时,要解决编译器提供的默认的拷贝构造和赋值构造无法解决的深浅拷贝问题。
  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FeatherWaves

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值