优秀的编程知识分享平台

网站首页 > 技术文章 正文

朝文分享(49):深入C++(十六)——运算符重载

nanyue 2025-01-20 15:36:47 技术文章 4 ℃

分享兴趣,传播快乐,增长见闻,留下美好!

亲爱的您,这里是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 新学苑

Tags:

最近发表
标签列表