网站首页 > 技术文章 正文
分享兴趣,传播快乐,增长见闻,留下美好!
亲爱的您,这里是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 新特性:使用示例
- 02-21走进git时代, 你该怎么玩?_gits
- 02-21GitHub是什么?它可不仅仅是云中的Git版本控制器
- 02-21Git常用操作总结_git基本用法
- 02-21为什么互联网巨头使用Git而放弃SVN?(含核心命令与原理)
- 02-21Git 高级用法,喜欢就拿去用_git基本用法
- 02-21Git常用命令和Git团队使用规范指南
- 02-21总结几个常用的Git命令的使用方法
- 02-21Git工作原理和常用指令_git原理详解
- 最近发表
- 标签列表
-
- cmd/c (57)
- c++中::是什么意思 (57)
- sqlset (59)
- ps可以打开pdf格式吗 (58)
- phprequire_once (61)
- localstorage.removeitem (74)
- routermode (59)
- vector线程安全吗 (70)
- & (66)
- java (73)
- org.redisson (64)
- log.warn (60)
- cannotinstantiatethetype (62)
- js数组插入 (83)
- resttemplateokhttp (59)
- gormwherein (64)
- linux删除一个文件夹 (65)
- mac安装java (72)
- reader.onload (61)
- outofmemoryerror是什么意思 (64)
- flask文件上传 (63)
- eacces (67)
- 查看mysql是否启动 (70)
- java是值传递还是引用传递 (58)
- 无效的列索引 (74)