当前位置: 首页> 房产> 建材 > 域名查询168_名片制作app软件_网站备案查询_网络营销方案

域名查询168_名片制作app软件_网站备案查询_网络营销方案

时间:2025/7/11 15:38:12来源:https://blog.csdn.net/2401_86275172/article/details/144649001 浏览次数:0次
域名查询168_名片制作app软件_网站备案查询_网络营销方案

-----------------------------------------------------begin------------------------------------------------------

运算符重载:

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

关键词:operator+(一种运算符)

运算符重载的特性:

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

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

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

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也是保险的,相当于加上保障,也是必要的~

运行结果:

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

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参数,所以你会产生“参数太多”的报错。

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


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个运算符不能重载:

赋值运算符重载:

误区:

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

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

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

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

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函数看看,代码实现如下:

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++】(剖析运算符重载和赋值运算符重载)理解的相关内容,蟹蟹你的阅读,希望可以对你有所帮助~

可以关注博主,今后多多指教,互相学习呀~

Rhzkp-CSDN博客

关键字:域名查询168_名片制作app软件_网站备案查询_网络营销方案

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: