In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, the editor will bring you how to understand C++ operator overloading. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
In C++, in addition to functions can be overloaded, in fact, operators can also be overloaded. We have been in contact with some before, and you may not be aware of it.
For example, the multiplication sign *, when applied to a pointer, means to take a value, and when used in arithmetic, it means multiplication. The same symbol, used in different places, has a different effect. This is actually a kind of overloading, and C++ decides which operation to use according to the number and type of operands.
In addition, C++ allows operator overloading to be extended to user-defined types, that is, structures and classes. For example, we can add the overload plus sign to two objects.
In fact, this kind of usage has also occurred, that is, the string class, when we add two strings, we get the concatenation of two strings.
We define an overloaded operator by adding the operator to the operator. It is important to note that we can only overload the operator that is already available in C++. For example, operator [] will overload the [] operator, operator + overload addition operator, and so on.
Let's look at an example:
# ifndef MYTIME0__H_#define MYTIME0__H_class Time {private: int hours; int minutes; public: Time (); Time (int h, int mx0); void AddMin (int m); void AddHr (int h); void Reset (int hog0, int massi0); Time Sum (const Time & t) const; void Show () const;}; # endif
We have created a Time class to represent time, along with some supporting methods. Let's focus on the Sum function, which receives a reference to a Time object and returns a Time object.
Let's take a look at the implementation of this function:
Time Time::Sum (const Time & t) const {Time sum; sum.minutes = minutes + t.minutes; sum.hours = hours + t.hours + sum.minutes / 60; sum.minutes% = 60; return sum;}
This logic indicates that the two times are added together, and you need to pay attention to carry. We set the passed parameter to reference to improve the efficiency of parameter passing. The returned result cannot be set to reference because the sum object is a local object and will be deleted at the end of the function, so the reference points to an object that does not exist.
We can rewrite this function as the overloaded addition operator:
Time Time::operator+ (const Time & t) const {Time sum; sum.minutes = minutes + t.minutes; sum.hours = hours + t.hours + sum.minutes / 60; sum.minutes% = 60; return sum;}
Except for a change in the function signature, the logic is the same as before.
When we call, in addition to using the function name, we can also use the plus sign to call:
Time a, B. opeator + (b); a + b
Both of these are possible and equivalent.
The above is the editor for you to share the C++ operator overload how to understand, if there happen to be similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.