网站首页 > 技术文章 正文
分享兴趣,传播快乐,增长见闻,留下美好!
亲爱的您,这里是LearningYard新学苑。
今天小编为大家带来“深入C++(十六)——运算符重载”,欢迎您的访问。
Share interest, spread happiness, increase knowledge, and leave beautiful.
Dear, this is the LearingYard Academy!
Today, Dive into C++ (16) - Operator overloading ,welcome to visit!
一、用途
1.Use
对于年龄问题,如妈妈今年20岁,女儿今年1岁,而爸爸今年的年龄是妈妈和女儿的年龄之和。但不想通过访问成员属性来赋值,想直接通过成员的加法来实现。
For example, the mother is 20 years old and the daughter is 1 year old, and the father's age this year is the sum of the ages of the mother and the daughter. But instead of accessing a member's property to assign a value, you want to do so directly by adding members.
但编译器并没有Person类之间的加法。
But the compiler doesn't have addition between Person classes.
二、实现
2.Implementation
所以,运算符重载就可以轻松解决这个问题。那么运算符重载要如何实现呢?其实跟函数定义大同小异,只要使用编译器提供的函数名,即“operator+要重载的运算符”,就可以实现。
Therefore, operator overloading can easily solve this problem. So how do you implement operator overloading? It's much the same as the function definition.You just use the function name provided by the compiler, which is "operator+ operator to override."
运算符的重载由两种形式,一种是作为成员函数中重载,另一种是作为全局函数中重载。
There are two forms of operator overloading, one is overloaded as a member function, and the other is overloaded as a global function.
需要注意的是,作为全局函数重载时,是两个参数。因为作为成员函数重载时,调用这个函数的对象的属性可以通过this指针直接访问。但全局函数不可以,需要传入两个对象。
Note that when overloaded as a global function, is two parameters. When overloaded as a member function, the properties of the object on which the function is called can be accessed directly via the this pointer. But a global function can't; we need to pass two objects.
无论是作为成员函数重载还是作为全局函数重载,都可以实现我们想要的对象之间的加法运算。
Whether overloaded as a member function or overloaded as a global function, we can achieve the desired addition between objects.
可是我以同样的方式解决另一个问题呢?比如我今年18岁,十年后我的年龄为多少?
But how can I solve the other problem in the same way? For example, if I am 18 years old, where will I be in 10 years?
我仍想通过运算符重载实现“Person + int”的运算。
I still want to implement the "Person + int" operation via operator overloading.
在重载的时候需要注意参数的数据类型。
You need to pay attention to the data types of the parameters when overloading.
三、本质
3.Essence
到这里,大家就可以发现。我们之前用的加减乘除等运算符实际上就是一个编译器内置的函数。只是编译器简化了调用这些函数的代码,方便程序员编写。
This is where you can find it. The addition, subtraction, multiplication, and division operators we used earlier are actually built-in compiler functions. It's just that the compiler simplifies the code that calls these functions for programmers to write.
四、注意
4.Pay attention
进行运算符重载时,有两点需要注意。其一是运算符重载无法改变编译器原本的内置函数。比如将加号的本质改成两数之差。其二是运算符重载不能乱用。比如说我要计算两者的年龄之和,却重载减号来实现加法功能。首先是逻辑上说不通,其次是代码的可读性差,其他人看不懂。
There are a couple of things to note when doing operator overloading. One is that operator overloading does not change the compiler's built-in functions. For example, change the essence of the plus sign to the difference between two numbers. The second is that operator overloading should not be misused. Let's say I want to calculate the sum of two ages, but I override the minus sign to add. The first is that the logic doesn't make sense, and the second is that the code isn't readable enough for other people to understand.
今天的分享就到这里了,
如果您对文章有独特的想法,
欢迎给我们留言。
让我们相约明天,
祝您今天过得开心快乐!
That's all for today's sharing.
If you have a unique idea about the article,
please leave us a message,
and let us meet tomorrow.
I wish you a nice day!
翻译:网易有道翻译
本文由LearningYard学苑整理并发出,如有侵权请后台留言沟通.
文案|Dongyang
排版|Dongyang
审核|hong
Learning Yard 新学苑
- 上一篇: 基于Clang库实现C++文件修改
- 下一篇: c++11新特性总结和boost库的使用
猜你喜欢
- 2025-01-20 C++|类型转换与运行时类型安全检查
- 2025-01-20 C++Qt开发——事件处理函数
- 2025-01-20 百度Linux C++后台开发面试题(个人整理)
- 2025-01-20 怎样才算学会了C++基础,一篇文章学习了解(包含Qt内容)
- 2025-01-20 C++通过aidl与Android系统服务通信(一)
- 2025-01-20 学习阅读C++编译器错误:函数声明中的荒谬错误
- 2025-01-20 朝文分享(54):深入C++(二十一)——多态
- 2025-01-20 c++多态
- 2025-01-20 深入探讨C++多线程性能优化
- 2025-01-20 【C++】C++ 11 新特性:使用示例
- 1507℃桌面软件开发新体验!用 Blazor Hybrid 打造简洁高效的视频处理工具
- 519℃Dify工具使用全场景:dify-sandbox沙盒的原理(源码篇·第2期)
- 489℃MySQL service启动脚本浅析(r12笔记第59天)
- 468℃服务器异常重启,导致mysql启动失败,问题解决过程记录
- 466℃启用MySQL查询缓存(mysql8.0查询缓存)
- 446℃「赵强老师」MySQL的闪回(赵强iso是哪个大学毕业的)
- 426℃mysql服务怎么启动和关闭?(mysql服务怎么启动和关闭)
- 423℃MySQL server PID file could not be found!失败
- 最近发表
- 标签列表
-
- c++中::是什么意思 (83)
- 标签用于 (65)
- 主键只能有一个吗 (66)
- c#console.writeline不显示 (75)
- pythoncase语句 (81)
- es6includes (73)
- windowsscripthost (67)
- apt-getinstall-y (86)
- node_modules怎么生成 (76)
- chromepost (65)
- c++int转char (75)
- static函数和普通函数 (76)
- el-date-picker开始日期早于结束日期 (70)
- js判断是否是json字符串 (67)
- checkout-b (67)
- localstorage.removeitem (74)
- vector线程安全吗 (70)
- & (66)
- java (73)
- js数组插入 (83)
- linux删除一个文件夹 (65)
- mac安装java (72)
- eacces (67)
- 查看mysql是否启动 (70)
- 无效的列索引 (74)