您现在的位置是:首页 > 编程 > 

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

2025-07-19 17:05:19
【C++】剖析运算符重载和赋值运算符重载 运算符重载: 当运算符被用于类类型的对象时,C++语言允许我们通过运算符重载的形式指定新的含义。C++规 定类类型对象使⽤运算符时,必须转换成调用对应运算符重载,若没有对应的运算符重载,则会编 译报错。为了让自定义类型 理解: 可以这么理解,当你需要使用类类型的对象来进行比较的时候,就一定一定需要用到运算符重载,要不然编译就会不通过 关键词:op

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

运算符重载:

当运算符被用于类类型的对象时,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==(ct Date& d1, ct 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,2);
	//另外一写法:d1(d2)
	d1 = d2;

	
	return 0;
}

要点:

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

运行结果:

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

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

bool operator==(ct Date& d1, ct 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,2);
	//另外一写法: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==(ct Date& d1, ct Date& d2) {
//		return d1._year == d2._year &&
//			d1._month == d2._month &&
//			d1._day == d2._day;
//	}
	bool operator==(ct 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,2);
	//另外一写法: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==(ct Date& d1, ct Date& d2) {
//		return d1._year == d2._year &&
//			d1._month == d2._month &&
//			d1._day == d2._day;
//	}
	bool operator=(ct 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,2);
	//另外一写法: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==(ct Date& d1, ct Date& d2) {
//		return d1._year == d2._year &&
//			d1._month == d2._month &&
//			d1._day == d2._day;
//	}
	void operator=(ct 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;
};

运行结果:

总结:

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

----------------------------------------------------ED--------------------------------------------------------

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

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

#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格

本文地址:http://www.dnpztj.cn/biancheng/1143559.html

相关标签:无
上传时间: 2025-07-19 09:26:17
留言与评论(共有 5 条评论)
本站网友 海南师范大学研究生
26分钟前 发表
重载运算符函数的参数个数和该运算符作用的运算对象数量⼀样多
本站网友 箭牌马桶怎么样
16分钟前 发表
他的名字是由operator和后⾯要定义的运算符共同构成
本站网友 魏公村口腔医院
15分钟前 发表
它也具有其返回类型和参数列表以及函数体
本站网友 内蒙古世纪男科医院
4分钟前 发表
C++语言允许我们通过运算符重载的形式指定新的含义