1. Definition of operator overload:
The operation objects of pre-defined operators in C ++ can only be basic data types. However, similar operations are also required for many user-defined types (such as classes. In this case, you must redefine these operators in C ++ and assign new functions to existing operators so that they can be used for specific operations of specific types. The essence of Operator Overloading is function overloading. It provides the scalability of C ++ and is also one of the most attractive features of C ++.
Operator Overloading is implemented by creating operator functions,An operator function defines the operations to be performed by an overloaded operator. The definition of an operator function is similar to that of other functions. The only difference is that the name of an operator function is composed of the operator keyword and the operator symbol to be overloaded. The general format of operator functions is as follows:
<Return type description> operator <operator symbol> (<parameter table>)
{
<Function body>
}
2. The following rules must be followed when operators are overloaded:
(1) Apart from class Relational operators ".", member pointer operators ". *", scope operators ":", sizeof operators, and three-object operators "? : ", All operators in C ++ can be overloaded.
(2) Heavy-duty operators are restricted to operators that allow heavy-duty within the range of existing operators in the C ++ language. New operators cannot be created.
(3) Operator Overloading is essentially a function overload. Therefore, the compiler selects Operator Overloading Based on the function overloading principle.
(4) The operator after the overload cannot change the operator's priority and combination, nor change the number and syntax structure of the operator's operands.
(5) An operator overload cannot change the meaning of the Operator Used for an object of internal type. It can only be used with user-defined objects, or when user-defined objects and internal objects are used together.
(6) Operator Overloading is an appropriate transformation to the original operator for new types of data. The overload function should be similar to the original function to avoid the use of overload operators without any destination.
(7) Functions with heavy-duty operators cannot have default parameters. Otherwise, the number of parameters of the operators is changed, which is in conflict with the first 3rd points;
(8) The reloaded operators can only be user-defined types. Otherwise, they will not be reloaded but will change the existing C ++ standard data type operator rules, will lead to great chaos in the world;
(9) user-defined class operators must be reloaded before they can be used, but with two exceptions, operators "=" and "&" do not need to be reloaded;
(10) Operator overloading can be performed in the form of member functions, but also through normal functions with non-member functions.
3. Operator Overloading:
There are two methods to overload operator functions: Reload is a member function of a class and load is a non-member function of a class. A non-member function is usually a friend. (You can overload an operator as a non-member or non-friend function. However, when such operator functions are private and protected, you must use the set data and read data functions provided in the public interface of the class. Calling these functions reduces performance. You can inline these functions to improve performance .)
1) member function Operators
The general format of a member function whose operator is overloaded as a class is:
<Function type> operator <operator> (<parameter table>)
{
<Function body>
}
When the operator is a member function of the class, the number of parameters of the function is one fewer than the original operand (except for the post-single-object operator ),This is because the member function uses the this pointer to implicitly access an object of the class and acts as the leftmost operand of the operator function. Therefore:
(1) When a binary operator is overloaded as a class member function, the function explicitly specifies only one parameter, which is the right operand of the operator.
(2) When the prepaid single-object operator is overloaded as a class member function, explicit parameter descriptions are not required, that is, the function does not have a form parameter.
(3) When the post-single-object operator is overloaded as a class member function, the function must carry an integer parameter.
The format of calling the member function operator is as follows:
<Object Name>. Operator <operator> (<parameter>)
It is equivalent
<Object Name> <operator> <parameter>
For example, A + B is equivalent to a. Operator + (B ). Generally, we use the regular expression of operators.
2) user functions Operator
The normal format of union functions whose operators are overloaded is:
Friend <function type> operator <operator> (<parameter table>)
{
<Function body>
}
When the operator is overloaded as a class's membership function, because there is no implicit this pointer, the number of operands remains unchanged,All operands must be passed through the function parameters. The function parameters correspond to the operands from left to right.
The format for calling the youyuan function operator is as follows:
Operator <operator> (<parameter 1>, <parameter 2>)
It is equivalent
<Parameter 1> <operator> <parameter 2>
For example, A + B is equivalent to operator + (A, B ).
4. Comparison of Two reload forms
In most cases, it is possible to overload operators as member functions of the class and membership functions of the class. However, member function operators and member function operators have their own characteristics:
(1)Generally,It is best to reload a single-object operator as a member function of the class, and a binary operator as a friend function of the class.
(2) The following binary operators cannot be overloaded as class friend functions: =, (), [],->.
(3) A type conversion function can only be defined as a member function of a class, rather than a friend function of a class.
(4) If an operator needs to modify the object state, it is better to select reload as a member function.
(5) If the required operands (especially the first operand) of the operator want to have implicit type conversion, you can only use the youyuan function.
(6)When an operator function is a member function,The leftmost operand (or only leftmost operand) must be a Class Object of the operator class (or a reference to this class object ).If the operand on the left must be an object of different classes,Or an internal object. This operator function must be implemented as a friend function.
(7) When the overload operator is required to be interchangeable, select the overload as a friend function.
5. instance:
1) Use member functions to overload operators:
# Include <iostream>
Using namespace STD;
Class X {
Int I;
Public:
X (int ii = 0) {I = II ;}
X operator + (const X & RX ){
I + = Rx. I;
Return X (I );
}
Int Geti () {return I ;}
};
Int main (){
X a (1), B (3 );
Cout <(a + B). Geti () <Endl;
Return 0;
}
2)UFIDA functions to overload Operators
Note: If you use
# Include <iostream>
Using namespace STD;
The following error occurs:
Fatal error c1001: Internal compiler Error
Therefore, use the header file: # include <iostream. h>
# Include <iostream>
Using namespace STD;
Class Complex {
Public:
Complex (double r = 0.0, double I = 0.0 ){
Real = R;
Image = I;
}
Friend complex operator + (const complex &, const complex &);
Void display ();
PRIVATE:
Double real;
Double image;
};
Complex operator + (const complex & C1, const complex & C2 ){
Return complex (c1.real + c2.real, c1.image + c2.image );
}
Void complex: Display (){
Cout <"(" <real <"," <image <"I)" <Endl;
}
Int main (){
Complex C1 (3, 4), C2 (5,-10), C3;
C3 = C1 + C2;
Cout <"C1 ="; c1.display ();
Cout <"C2 ="; c2.display ();
Cout <"C1 + C2 ="; c3.display ();
Return 0;
}
Detailed explanation of Operator Overloading