C++ operator overloading

1 Concept

The so-called overloading is to give a new meaning again. Function overloading is to give a new meaning to an existing function and make it realize new functions. Therefore, a function name can be used to represent functions with different functions, that is, "one name is used for multiple purposes".

Operators can also be overloaded. In fact, we are already using operator overloading without knowing it. For example, everyone is accustomed to using the addition operator "+" to add integers, single-precision numbers and double-precision numbers, such as 5+8, 5.8 +3.67, etc. In fact, computers are used to integer, single-precision numbers and double-precision numbers. The addition operation process is very different, but since C++ has overloaded the operator "+", it can be applied to int, float, doUble type operations.

Another example is "<<" is the displacement operator (left shift) in C++'s bit operation, but it is a stream insertion operator used in conjunction with the stream object cout in the output operation, and ">>" is also a displacement operator (right shift). Shift), but in the input operation is the stream extraction operator used with the stream object cin. This is operator overloading. The C++ system overloads "<<" and ">>", and when users use them in different situations, their effects are different. The overload processing of "<<" and ">>" is placed in the header file stream. Therefore, if you want to use "<<" and ">>" as stream insertion operator and stream extraction operator in the program, you must include the header file stream (of course, "using namespace std") in this file module.

2 Limitations of operator overloading

The overloaded operator function can make a new interpretation of the operator, but the original basic semantics remain unchanged:
1. Does not change the precedence of the operator
2. Does not change the associativity of the operator
3. Does not change the required operator Operand
4. No new operator is created.

3 Basics of Operator Overloading Programming

There are two ways of operator overloading: member function or friend function overloading.
The key is that member functions have this pointer, while friend functions do not have this pointer.

3.1 Steps to define operator overloaded function names

Global function, class member function method to implement operator overloading Steps
1) To recognize that operator overloading is a function, write the function name operator+ ()
2) According to the operand, write the function parameters
3) According to the business, improve the function return value (see function whether to return a reference or a pointer element), and implement function business

3.2 Friend function operator overloading

1) Friend function and member function selection method
 When the class of the left operand cannot be modified, use the global function for overloading
 The =, [], () and -> operators can only be overloaded through member functions

2) Overload the << >> operator with friend function
 istream and ostream are predefined stream classes of C++
 cin is the object of istream, cout is the object of ostream
 The operator << is overloaded by ostream as an insertion operation, using For outputting basic type data
 Operator>> is overloaded by istream as extraction operation, used for inputting basic type data
 UF function overloads << and >> to output and input user-defined data types

a) Implement << operator
ostream& operator<<(ostream &out, Complex &c1)
{
//out<<"12345, life is really hard"<

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325485662&siteId=291194637