The operand of a predefined operator in C + + can only be a basic 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 that enables them to perform specific operations on specific types. The essence of operator overloading is function overloading, which provides the extensibility of C + + and is one of the most attractive features of C + +.
Operator overloading is implemented by creating operator functions, which define the operations that the 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 made up of the keyword operator and the operator symbol to be overloaded later. The general format of the operator function definition is as follows:
< return type descriptor > operator < operator symbol > (< parameter table >)
... {
< function body >
}
The following rules apply to operator overloading:
(1) Except for the generic relational operator ".", the member pointer operator ". *", The scope operator "::", the sizeof operator and the three-mesh operator "?:", all operators in C + + can be overloaded.
(2) Overloaded operators restrict the allowed overloaded operators in the range of operators that are already in the C + + language, and cannot create new operators.
(3) Operator overloading is essentially a function overload, so the compiler's choice of operator overloading follows the selection principle of function overloading.
(4) An overloaded operator cannot change the precedence and binding of an operator, nor can it change the number of operator operands and the syntax structure.
(5) Operator overloading cannot change the meaning of the operator for an intrinsic type object. It can only be used with objects of user-defined types, or when mixed with objects of the user-defined type and internal types.
(6) Operator overloading is the proper modification of the original operator for the actual need for the new type of data, and the overloaded functionality should be similar to the original functionality, avoiding the use of overloaded operators without destination.
operator function overloading generally has two forms: overloading the member functions of the class and overloading the non-member functions of the class. A non-member function is usually a friend. (an operator can be overloaded as a non-member, non-friend function.) However, when such an operator function accesses private and protected members of a class, you must use the set data and functions that are available in the public interface of the class to read the data, which can degrade performance when called. These functions can be inline to improve performance. )
member function operators
The general format of operator overloading for member functions 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 number of arguments to the function is one less than the original operand (except for the post-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. So:
(1) When the binocular operator is overloaded as a member function of a class, the function explicitly describes only one parameter, which is the right operand of the operator.
(2) When the pre-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 the post-order operator is overloaded with a member function of a class, the function takes an integer parameter.
The calling member function operator is in the following format:
< object name >.operator < operator > (< parameters >)
It is equivalent to
< object names >< operators >< parameters >
For example: A+b is equivalent to A.operator + (b). In general, we use the idiomatic expressions of operators.
Friend function operators
The general format of the friend function for an operator overload for a class is:
Friend < function type > operator < operator > (< parameter table >)
... {
< function body >
}
When an operator is overloaded with a friend function of a class, because there is no implied this pointer, the number of operands does not change, and all operands must be passed through the function's formal parameters, and the arguments of the function 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 types of overloaded forms
In most cases, overloading the operator with the member function of the class and the friend function of the class is possible. But the member function operator and the friend function operator also have their own characteristics:
(1) In general, the monocular operator is best to overload the member function of the class; The binocular operator is best to overload the friend function of the class.
(2) Some of the following binocular operators cannot be overloaded with the friend function of a class: =, (), [], and.
(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 an object, it is preferable to select overload for the member function.
(5) If the operand required by the operator (especially the first operand) wishes to have an implicit type conversion, only the friend function is selected.
(6) When an operator function is a member function, the leftmost operand (or only the leftmost operand) must be a class object of the operator class (or a reference to the class object). If the operand on the left must be a non-homogeneous object, or an object of an intrinsic type, the operator function must be implemented as a friend function.
(7) When an overloaded operator is required to be exchangeable, select overload as a friend function.
Operator overloading for C + + (GO)