前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >【C++】剖析运算符重载和赋值运算符重载

【C++】剖析运算符重载和赋值运算符重载

作者头像
用户11456817
发布2025-01-22 08:49:15
发布2025-01-22 08:49:15
10100
代码可运行
举报
文章被收录于专栏:学习学习
运行总次数:0
代码可运行
运算符重载:

当运算符被用于类类型的对象时,C++语言允许我们通过运算符重载的形式指定新的含义。C++规

定类类型对象使⽤运算符时,必须转换成调用对应运算符重载,若没有对应的运算符重载,则会编

译报错。为了让自定义类型

理解: 可以这么理解,当你需要使用类类型的对象来进行比较的时候,就一定一定需要用到运算符重载,要不然编译就会不通过

关键词:operator+(一种运算符)
运算符重载的特性:
  • 运算符重载是具有特殊名字的函数,他的名字是由operator和后⾯要定义的运算符共同构成。和其 他函数⼀样,它也具有其返回类型和参数列表以及函数体。
  • 运算符重载是具有特殊名字的函数,他的名字是由operator和后⾯要定义的运算符共同构成。和其他函数⼀样,它也具有其返回类型和参数列表以及函数体。
  • 重载运算符函数的参数个数和该运算符作用的运算对象数量⼀样多。⼀元运算符有⼀个参数,⼆元运算符有两个参数,二元运算符的左侧运算对象传给第⼀个参数,右侧运算对象传给第⼆个参数。
  • 运算符重载以后,其优先级和结合性与对应的内置类型运算符保持⼀致。

运算符重载与普通函数返回类型与参数列表类似,也同样具有自己的返回类型,参数。

下面是一个简单的代码例子:

代码语言:javascript
代码运行次数:0
复制
class Date
{
public:
	Date(int year = 1, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
public://这里需要改成公开的,要不然运算符重载访问不到
//private:
	int _year;
	int _month;
	int _day;
};

bool operator==(const Date& d1, const Date& d2) {
	return d1._year == d2._year &&
		d1._month == d2._month &&
		d1._day == d2._day;
}

int main() {
	Date d1(2024, 12, 22);
	Date d2(2024.12,23);
	//另外一写法:d1(d2)
	d1 = d2;

	
	return 0;
}

要点:

  • 这里的参数需要使用引用,且需加上const进行修饰
  • 而且这里是自定义类型传参,使用传值传参的话,就会调用一次拷贝构造,所以这里最好使用引用传参
  • 我们不需要对函数体内进行修改。加上const也是保险的,相当于加上保障,也是必要的~

运行结果:

这是放入类域之外的情况下,当我们将其放入类域中时: 代码例子如下:

代码语言:javascript
代码运行次数:0
复制
class Date
{
public:
	Date(int year = 1, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}

bool operator==(const Date& d1, const Date& d2) {
		return d1._year == d2._year &&
			d1._month == d2._month &&
			d1._day == d2._day;
	}
public://这里需要改成公开的,要不然运算符重载访问不到
//private:
	int _year;
	int _month;
	int _day;
};


int main() {
	Date d1(2024, 12, 22);
	Date d2(2024.12,23);
	//另外一写法:d1(d2)
	d1 = d2;

	
	return 0;
}

会发现报错啦!

不过没事,凡事遇到不要慌,特别是报错,只会让我们更加兴奋~

参数太多:这个的原因是因为,放在类域中的话,它会自动给你加入一个隐含的this参数,所以你会产生“参数太多”的报错。

所以我们做下面的这种操作即可

代码语言:javascript
代码运行次数:0
复制
class Date
{
public:
	Date(int year = 1, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}

//bool operator==(const Date& d1, const Date& d2) {
//		return d1._year == d2._year &&
//			d1._month == d2._month &&
//			d1._day == d2._day;
//	}
	bool operator==(const Date& d1) {
		return _year == d1._year &&
			_month == d1._month &&
			_day == d1._day;
	}
public://这里需要改成公开的,要不然运算符重载访问不到
//private:
	int _year;
	int _month;
	int _day;
};


int main() {
	Date d1(2024, 12, 22);
	Date d2(2024.12,23);
	//另外一写法:d1(d2)
	d1 = d2;

	
	return 0;
}

这样就轻松解决啦~

还可以这样写:
以下5个运算符不能重载:
赋值运算符重载:
误区:

这里应该有很多小伙伴都会把赋值运算符重载和拷贝构造函数搞混:

赋值运算符:用于完成两个已经存在的对象直接的拷贝赋值

拷贝构造函数:用于⼀个对象拷贝初始化给另⼀个要创建的对象

以下是根据赋值运算符(=)来进行重载的代码:

代码语言:javascript
代码运行次数:0
复制
class Date
{
public:
	Date(int year = 1, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}

//bool operator==(const Date& d1, const Date& d2) {
//		return d1._year == d2._year &&
//			d1._month == d2._month &&
//			d1._day == d2._day;
//	}
	bool operator=(const Date& d1) {
		return _year == d1._year &&
			_month == d1._month &&
			_day == d1._day;
	}
public://这里需要改成公开的,要不然运算符重载访问不到
//private:
	int _year;
	int _month;
	int _day;
};


int main() {
	Date d1(2024, 12, 22);
	Date d2(2024.12,23);
	//另外一写法:d1(d2)
	d1 = d2;
	//也可以写成:
	
	
	return 0;
}

运行结果如上,没有问题

我们再调用一下print函数看看,代码实现如下:

代码语言:javascript
代码运行次数:0
复制
class Date
{
public:
	Date(int year = 0, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}

//bool operator==(const Date& d1, const Date& d2) {
//		return d1._year == d2._year &&
//			d1._month == d2._month &&
//			d1._day == d2._day;
//	}
	void operator=(const Date& d) {
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}

	void Print() {
		cout << _year << "-" << _month << "-" << _day << endl;
	}
//public://这里需要改成公开的,要不然运算符重载访问不到
private:
	int _year;
	int _month;
	int _day;
};

运行结果:

总结:

刚开始学习的时候,我也很容易将运算符重载和赋值运算符重载弄混,很伤脑筋,但其实是要想想一个是比较的,一个是赋值的,就可以理解了。其实最容易弄混的是赋值运算符重载和拷贝构造函数,刚开始的时候不懂他们的区别,经过理解还是相对容易哒~

----------------------------------------------------END--------------------------------------------------------

以上就是我分享的我对【C++】(剖析运算符重载和赋值运算符重载)理解的相关内容,蟹蟹你的阅读,希望可以对你有所帮助~

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-01-21,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 运算符重载:
  • 关键词:operator+(一种运算符)
  • 运算符重载的特性:
  • 还可以这样写:
  • 以下5个运算符不能重载:
  • 赋值运算符重载:
  • 误区:
  • 总结:
  • ----------------------------------------------------END--------------------------------------------------------
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档