The operations object of a predefined operator in C + + can only be the base data type. In practice, however, similar operations are required for many user-defined types, such as classes. These operators must be redefined in C + +, giving the existing operators new functionality to enable them to perform specific operations on specific types. The essence of operator overloading is function overloading, which provides scalability for C + + and is one of the most attractive features of C + +.
Operator overloading is implemented by creating operator functions that define the operations that overloaded operators will take. The definition of an operator function is similar to that of other functions, except that the function name of the operator function is composed of the keyword operator and the operator symbol that is subsequently overloaded. The general format of the operator function definition is as follows:
< return type descriptor > operator < operator symbol > (< parameter table >)
{
< function body >
}
The following rules are followed for operator overloading: (1) All operators in C + + can be overloaded except for the generic relational operator ".", The scope operator "::", the sizeof operator, and the three-mesh operator "?:".
(2) Overloaded operators are restricted to overloaded operators within the range of operators in the C + + language, and new operators cannot be created.
(3) Operator overloading is essentially a function overload, so the compiler's choice of operator overloading follows the selection principle of function overloading.
(4) The operator after overloading cannot change the precedence and binding of operators, nor can it change the number of operator operands and the grammatical structure.
(5) Operator overloading cannot change the meaning of the operator for an intrinsic type object. It can only be used with objects of a user-defined type, or when mixed with objects of a user-defined type and an object of an internal type.
(6) Operator overloading is an appropriate modification of the original operator for the actual needs of the new type of data, and the overloaded functionality should be similar to the original functionality, avoiding overloading operators without destinations.
There are generally two forms of operator function overloading: overloaded as a member function of a class and a non-member function that overloads as a class. Non-member functions are usually friends. (You can overload an operator as a non-member, non-friend function.) However, when such operator functions access the private and protected members of a class, it is necessary to use the set of data and the functions that read the data provided in the public interface of the class, which degrades performance. These functions can be inline to improve performance. )
The general format of the member function operator operator overloading the member function of a class is:
< function type > operator < operator > (< parameter table >)
{
< function body >
}
When an operator is overloaded with a member function of a class, the function has one less number of arguments than the original operand (except the back-order operator), because the member function implicitly accesses an object of the class with the this pointer, which acts as the leftmost operand of the operator function. Therefore: (1) when the binocular operator is overloaded with a member function of a class, the function explicitly describes only one parameter, which is the right-hand operand of the operator.
(2) When the predecessor of a single order operator is overloaded as a member function of a class, there is no need to explicitly describe the parameter, that is, the function has no formal parameters.
(3) When a back-order operator is overloaded with a member function of a class, the function takes an integer parameter.
The format of the calling member function operator is as follows:
< object name >. Operator < operator > (< parameter >)
It is equivalent to
< object name >< operator >< parameter >
For example: A + B is equivalent to a. operator + (b). In general, we use the idiomatic expressions of operators.
The general format of the friend function operator overload for a friend function of a class is:
Friend < function type > operator < operator > (< parameter table >)
{
< function body >
}
When an operator is overloaded as a friend function of a class, because there is no implied this pointer, the number of operands is unchanged and all operands must be passed by the function's parameters, and the function's arguments correspond to the operands from left to right. The format of the function operator is as follows:
Operator < operator > (< parameter 1 >, < parameter 2 >)
It is equivalent to
< parameter 1 >< operator >< parameter 2 >
For example: A + B is equivalent to operator + (A,B).
Comparison of two kinds of overloaded forms
In most cases, it is possible to overload an operator as a member function of a class and a friend function of a class. However, the member function operators and the friend function operators also have their own characteristics: (1) In general, the monocular operator is best overloaded as a member function of the class, and the binocular operator is best overloaded as a friend of the class function.
(2) Some of the following binocular operators cannot be overloaded as friend functions of the class: =, (), [],->.
(3) A type conversion function can only be defined as a member function of a class and cannot be defined as a friend function of a class.
(4) If the operation of an operator needs to modify the state of the object, select the overload to be a member function better.
(5) If the operand required by the operator (especially the first operand) wants an implicit type conversion, only the friend function can be selected.
(6) When the operator function is a member function, the leftmost operand (or only the leftmost operand) must be a class object (or a reference to the class object) of the operator class. The operator function must be implemented as a friend function if the left-hand operand must be an object that is not homogeneous, or an object of an intrinsic type.
(7) When the overload operator is required to be interchangeable, the overload is selected as a friend function.