I. Reload basic rules
1. The number of parameters of the overload operator should be the same as that of the operator.
The overload operator acts as a member function of the class, and an implicit * This acts as the first function of the function.
2. After heavy load, the priority, combination, and number of operations remain unchanged. However, the order of values cannot be guaranteed.
The order of value is: Which of the following values is obtained first when the priority and combination are the same?
3. cannot create new operators
4. the overload operator must have at least one class type or enumeration type.
5. Operators that cannot be overloaded: (1): (2). (3 )? : (4 )#
Operators that can be a dollar or binary: (1) + (2)-(3) * (4 )&
The default real parameter operator can be used: (1 )()
6. When the operator is used as a non-member function overload, it is often necessary to declare the Friends of this class. Unless the data member of this class is public
7. In some special cases, the assignment operator must release some old values before assigning new data based on the new value type. In this case, an error occurs during auto-copy.
8. The operator keyword is used together with the type to be converted to form an overloaded function of the conversion operator. This function does not return values, but can return a converted value in the function.
2. [] Operator Overloading example
1) The overloaded [] operator is only applicable to objects of this class.
2) The reload must be a non-static member.
3) You cannot reload it as a friend function.
4) Advantages of heavy load: you do not need to define the array length as a constant; you can avoid crossing the border.
char& operator[](int i){if(i >= 0 && i < length)return size[i];else{cout<<"error"<<endl;return size[length-1];}}
3. Example 1) Example 1) return result of pre-auto-plus and post-auto-plus.
Post-auto-increment return by value, and the return is a temporary object.
2) parameters in the post auto-increment are meaningless, just to distinguish the front-end
3) It is obvious that the front-end self-acceleration is more efficient and you should try to use the front-end
Class A {int X; public: A (int I): X (I) {cout <"construct" <This <Endl;} A (const A &) {x =. x; cout <"copy" <This <Endl ;}~ A () {cout <"delete" <This <Endl;} int get () {return X;} void set (int I) {x = I ;} // pre-auto-increment const A & operator ++ () {++ X; return * This;} // post-auto-increment const a operator ++ (int o) {a temp (* This); ++ X; return temp ;}}; int main () {A (1); cout <. get () <Endl; a B = ++ A; cout <B. get () <Endl; cout <. get () <Endl; B = A ++; cout <B. get () <Endl; cout <. get () <Endl; return 0 ;}
Output:
Construct 0012ff38
1
Copy 0012ff34
2
2
Copy 0012feb8
Copy 0012ff30
Delete 0012feb8
Delete 0012ff30
2
3
Delete 0012ff34
Delete 0012ff38
4. Input and Output Operator Overloading Example 1. Output Operator Overloading
ostream& operator<<(ostream& s, const A& a){s<<a.rx<<' '<<a.ry<<endl;return s;}
2. Input operator overload
istream& operator>>(istream& s, A& a){s>>a.rx>>a.ry;return s;}
3. determine whether it is a stream operator or a shift operator based on the object type on the left of the operator
4. The ostream class does not have a public replication constructor. Therefore, this function cannot call the replication constructor of this class to copy objects. ostream objects must be accepted by reference and returned by reference.
5. iostream is an application that inherits multiple types of data.
6. stream operators cannot be reloaded as a member function of a class because they contain other objects. You can only reload it as a friend function.
7. Example:
Int A = 3;
Cout <++ A <a ++ <Endl;
Output: 53
Explanation:
When a parameter passed in by a function is not a simple variable but an arithmetic expression, evaluate the expression and add the value to the stack.
The order of evaluation is from right to left, so first a ++
1) execute a ++, A = 4, and return 3
2) execute ++ A, A = 5, and return 5
3) execute cout <return value (5) <return value (3) <Endl;