Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the limit of C++ operator overloading

2025-01-17 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 about the overloading restrictions of C++ operators. 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.

I. restrictions on overloading

In the last article, we talked about overloading operators in classes and structures. operator overloading is not arbitrary. C++ gives some restrictions to ensure the specification and the accuracy of the program.

Let's take a look at one by one:

1. At least one Operand must be of a user-defined type

It doesn't matter if we don't understand this sentence, we just need to remember its purpose. Its main purpose is to prevent users from overloading operators for standard types. For example, overload + as the difference between two numbers instead of and.

This restriction has a certain impact on creativity, is not so flexible, but can ensure the normal operation of the program. There will be no anti-human situation.

two。 Cannot violate the original rules of the operator

This is easy to understand, such as the + sign, which calculates the sum of two numbers. Two operands are required, and now it is illegal for us to reload it into one Operand.

P / P

Similarly, we cannot change the priority of the operator. If the plus operator is overloaded into the addition of two classes, the new operator has the same priority as the original plus sign.

3. Cannot create new operator

I've said this before, for example, you can't define operators like operator @ that don't exist before.

4. Prohibited list

Some operators prohibit overloading, such as: sizeof,.,:,:, typeid, const_cast, dynamic_cast, reinterpret_cast, static_cast

These operators often have special functions that prohibit overloading directly from the implementation level.

5. Some operators can only be overloaded by member functions

Most operators can be overloaded through member functions and non-member functions, but with some exceptions, they can only be overloaded through member functions

Such as:

=: assignment operator

(): function call operator

[]: subscript operator

->: arrow symbol

The non-member function here seems a little confusing, which we'll encounter later, which mainly refers to the friend function.

The above is the C++ operator overload limit shared by the editor. If you happen to have 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report