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

How to implement overload operator in C++

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

The editor of this article gives you a detailed introduction of "how to implement the overload operator in C++". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to implement the overload operator in C++" can help you solve your doubts. Let's follow the editor's ideas to learn new knowledge.

one. The timing of overloading operators

one. Overload the assignment operator when you need to assign values to each other between defined objects

two. You need to overload the arithmetic operator when adding arithmetic attributes to the numeric type

three. Overload the relational operator when you need to make a logical comparison for a defined object

four. For container, overload the subscript operator []

five. When you need to read and write objects from the I / O stream, the operator is overloaded.

six. Overload the member pointer operator-> to implement smart pointers

seven. Overload the new,delete operator in a few cases

eight. Do not overload other operators

Virtually any work done with overloaded operators can be done using member functions.

The overloaded operator may not necessarily be related to the original operator. For example, when I overload the groom, I can concatenate the string without adding. Of course, it's OK if you use the\'+ operator to do subtraction, but it's not a wise move.

two. Rules for overloading operators

one. Overloaded operators cannot violate the syntax rules of the language

two. If an operator can be placed between two operands, it can be overloaded to meet the needs of class operations, even if this usage is not acceptable to the compiler.

three. Cannot create operators that are not available in the C++ language

four. The following operators cannot be overloaded

. Class member operator

. * member pointer operator

:: domain parsing operator

Conditional expression operator

five. The priority of the operator cannot be changed when overloaded

three. Operator overloading

Operator overloading is achieved by overloading operator functions. For each operator @, there is an operator function operator@, in C++, where @ is the various operators of C++.

The general prototype of the operator function is:

Ype operator@ (arglist)

Where type is the type of operation result and arglist is the list of operands.

After reading this, the article "how to implement the overloaded operator in C++" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it. If you want to know more about the article, welcome to 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