In the following article, we record that, after studying the C ++ primer operator reload chapter and integrating with the knowledge of operator reload, the content of Operator Overloading is not very deep. It mainly involves the frequent use of operator overloading, I did not do any in-depth research, because I think that even if I spend some time researching them, they will not always be used in the future, these difficult knowledge points are gradually forgotten, so it is better to focus on the basic knowledge of operator overloading and the most common knowledge points, it is time to learn more about them and learn how to use them. Of course, this is only one of my views on common and frequently-used knowledge points. If you can understand it all, it is certainly the best, but if you have limited time, I think it is most important to thoroughly understand what is important. As the saying goes: good steel should be used on the blade .... Well, I have talked a lot about it. Now I want to go into the subject: 1. What is Operator Overloading? When you see the overload, it is easy to think of the member function overload. The function overload can make the function with the same name have different actual functions, you only need to assign different parameters to these functions with the same name, and the operator overload is based on this mechanism. The system provides many operators, such as "+" and "[]". These operators have some default functions, the operator overload mechanism allows us to assign different functions to these operators, and to use their own function-defined operators (that is, overloaded operators) according to the format used by common operators ). After definition, we can use our own overload operators in the format of common operators. Operator Overloading is generally defined within a class, just like a member function. This is called a class member overload operator. Of course, it can also be defined outside the class, that is, the non-class member operator is overloaded. Ii. Why use Operator Overloading? For example, the class string has such a function that connects two strings into one. Therefore, we can define a member function for the class string to implement this function, you can give the function an image name, such as concatenate or append. However, the two names are not as intuitive as the operator "+ =. In this case, we can define the overload of the operator "+ =" to implement this function. That is to say, if you want to define a function, and the function of this function is similar to the function of the operator, we can define the overload operator at this time, instead of using the common member function definition. Here, the operator overloading refers to the overloading of the operator defined by the system, rather than defining two "+ =", which requires clarity. However, these four operators cannot be used for overloading :::*?: 3. How to declare Operator Overloading? It is similar to a common function, except that its name includes the keyword operator and a predefined operator followed by it. For example: string & operator + = (const string &); string & operator + = (const char *); note: the brackets above represent formal parameters, even if the operator is overloaded without parameters, you should also write an empty "()" instead of omitting it. This is actually similar to the declaration of common functions. In fact, the only difference between declarations is
The name is different.. 4. How to Use Operator Overloading? Two types of Operator Overloading: class member Operator Overloading and non-class member Operator overloading. 1. The class member operator reloads two "=" operator reloads (bool operator = (const char *) const) in the string of a known class; bool operator = (const string &) const; the first overload operator allows us to compare whether a string class object is equal to a C-style string, the second one allows us to compare whether two string class objects are equal. Sample Code: # include <string. h> int main () {string flower; If (flower = "Lily") // correct: Call bool operator = (const char *) const ;...... Else if ("Tulip" = flower) // error .......} The key is, why is the second overload operator incorrect? Because: only when the left operand is an object of the class type, the overload operator as a class member will be considered. Because the "Tulip" here is not a string-type object, the compiler tries to find a built-in operator that can have a left operand of a C-style string. However, this operator does not actually exist, therefore, an error occurs during compilation. Q: We can use the constructor of the string class to convert a C-style string into a string object. why can't the compiler perform the above conversions? That is, if (string ("Tulip") = flower); // This is the correct answer: for efficiency and correctness, the overloaded operators do not require the same type of the two operands. There may be such a text class. The constructor parameters of this class and the parameters of its member overload operators are consistent with those of the string class, if you enable the compiler to automatically convert a C-style string to a certain type of object, the compiler first Retrieves all class definitions and selects classes that provide correct constructors and overloaded operators for conversion, this will undoubtedly increase the Compilation Time of the program. In addition, both the string class and the text class are suitable. The Compiler does not know whether to convert the C-style string into a string or a text object. For class member overload operators, the implicit this pointer is used as the first implicit parameter. For member operators, flower = "Lily" will be rewritten by the compiler as: Flower. operator = ("Lily"); 2. To solve the problem above, we can consider using non-class member operators instead of class member operators, the advantage of this is that the left operand does not have to be a type object of a class. For operators that require two operands to be overloaded, we can define two parameters. For example, bool operator = (const string &, const string &); bool operator = (const string &, const char, the two global overload operators have one parameter more than the member operators. After this definition, the above code is used. When flower = "Lily" is called, the above bool operator = (const string &, const char *); is called *);. However, "Tulip" = which operator will flower call to overload it? We have not defined bool operator = (const char *, const string &);, do we have to define such a global operator overload? The answer is no, because when an overload operator is a namespace function, both the first and second parameters of the operator, that is, the right and right operands of the operator, will be considered for conversion, just like int Vi = 1; double Vd = 2.0; VI = vi + VD; Convert VD to int type first, and then add. This means that, the compiler will explain the second usage as follows: bool operator = (string ("Tulip"), flower ). This will increase the System Conversion overhead. Therefore, if you need to frequently compare C-style strings and string objects, it is best to define the operator overload above. If not, we only need to define the following one: bool operator = (const string &, const string &); analysis: When to define class member Operator Overloading and when to define non-class member Operator Overloading? A: (1) If an overload operator is a class member, it will be called only when the left operand used with it is the class object, if the left operand of this operator must be of another type, the overload operator must be overloaded by non-class member operators. (2) c ++ requirements, value assignment (=), subscript ([]), call (), and member access arrow (->) the operator must be specified as a class member operator; otherwise, it is incorrect. We can only define overload operators for class or enumeration type operands. We can achieve this: declare the overload operators as class members, it can also be declared as a non-class member overload operator, but there must be at least one parameter of the class or enumeration type at the same time (transmitted by value or by reference ). Actually, we cannot modify the built-in operator overload or add the operator overload. For example, the following statement is incorrect: int operator + (INT, INT ); this changes the + operator function of the built-in int type, so it is incorrect. Operator Overloading related knowledge points: 5. Friend (friend) Considering that class member operators can overload private variables in the category class, non-class member overload operators cannot easily include private members of the category class, for convenience, we can use friend (friend) to facilitate private members of the category class. Example: Class string {friend bool operator = (const string &, const string &); friend bool operator = (const string &, const char *); public: //........ PRIVATE ://.........} Note: The friend declaration follows the class name rather than in public, private, or protected, because youyuan is not a member of the authorization class and this keyword can only appear in the class. After the above declaration, our non-class member overload operator can directly orientation The Private Members of the string class. Of course, we can also avoid using youyuan, but it is also possible to indirectly access private members of this class through the common member functions of this class. The inline function is good and the efficiency is not low. From this point of view, the Declaration of friend (friend) is mainly to facilitate the efficient private member variables of the member class. Analysis: When should I use youyuan: (1) a class does not provide public functions to access private members. (2) When using a common member function to access private member variables, the efficiency is relatively poor. Except for the overload operators used by non-members, youyuan is a namespace function (such as a global function), a member function of the class defined before this, or a complete class, can be declared as a member of a class. The following declaration: Class B; Class A {friend Class B; public ://....... PRIVATE ://......} Through the above declaration, member functions of Class A can access all private members of Class B, and member functions of Class B can access private members of all Class. 6. implicit type conversion for class objects we know that the built-in types provided by the system have the implicit type conversion function. In fact, we can also provide implicit type conversion for classes we write. In this way, when we use this type of object, the compiler automatically calls the type conversion function of this class to implement the type conversion function. C ++ provides such a mechanism through which each class can define a group of "conversions that can be applied to objects of this type". For example: Class smallint {public: smallint (INT ival): Val (ival) {}; operator int () {return value ;}; // type conversion operator smallint-> int PRIVATE: int value ;} in the preceding example, the INT () operator is a conversion function that defines a user conversion to implement conversion between the class type and the type specified in the conversion function. In this example, the target type is int. After the above definition, the smallint object can be used in any place where int can be used, for example: smallint Si (3); Si + 3.1415926; in this way, the smallint conversion function is called first, generate an int value of 3, and then change 3 to 3.0, and add it to 3.1415926. In fact, another benefit of defining the Conversion Function in the above class is that the overload operator is omitted for this class, especially when the parameter types of the overload operator are diverse. 1. Conversion Function Format: operator type (); // The parameter must be null, and () cannot be omitted, and no return value type. Description: type indicates the target type. It can be replaced by a built-in type, class type, or typedef name, but cannot represent an array. The conversion function must be a member function of the class, the return type and parameter table cannot be specified. Explicit forced type conversion will call the conversion function. If the type of the converted value is a class type, it has a conversion function, in addition, if the type of the Conversion Function is specified by force conversion, the forced conversion function of this class is called. For example, char * tokname = static_cast <char *> (Tok); // Tok indicates the object, and its class provides a conversion function to char. 2. Using constructors as conversion functions in a class constructor, Any constructor with only one parameter, such as smallint constructor smallint (INT ), both define a set of implicit conversions to convert the parameter type of the constructor to this type. Note: Convert int type to smallint type. The function is the opposite of the previously mentioned function. For example, void calc (smallint); int VI; calc (VI); then, the compiler will implicitly call the smllint-like constructor smllint (INT) and convert VI into smallint objects, then pass the object to the function calc (). It can be understood as follows: {smallint temp = smallint (I); calc (temp);} the braces indicate the life cycle of the temporary object temp. Note: The Compiler does not use an explicit Constructor (the explicit it flag keyword) to perform implicit type conversion, but can use such constructor for forced conversion (that is, static_cast <> ). This is just my own understanding of operator overloading. It is quite simple and common. I didn't discuss specific operator overloading. I will fill them out later, well, let's write so much about it first. Please pay attention to the mistakes made by netizens.