What is an overload of an operator?
Operators are combined with classes to produce new meanings.
Why to introduce operator overloading.
Function: To implement the polymorphism of a class (polymorphism means that a function name has many meanings)
How to implement overloading of operators.
Method: A member function of a class or a friend function (a normal function outside of a class)
Rule: Operators that cannot be overloaded have. And. * and?: and:: and sizeof
The use of friend functions and member functions: In general, we recommend that unary operators use member functions, and the two-dollar operator uses the friend function
1, operator operations need to modify the state of the class object, then use the member function. Operators (such as =,+=,++) that need to do the left-hand operand
2, when the operation, there are several mixed with the object operation, you must use the friend
In the 3, two-dollar operator, you must use a friend function when the first operand is not an object. such as input and output operators << and >>
The specific rules are as follows:
Operator |
Recommended Use |
All unary operators |
member functions |
= () []-> |
Must be a member function |
= = =/= *= ^= &=!=%= >>= <<=, it seems that the equals are here. |
member functions |
All other two-dollar operators, such as: –,+,*,/ |
Friend function |
<< >> |
Must be a friend function |
2. Parameters and return values
When the parameters are not changed, they are generally passed by the const reference (if you use the member function overload, the function is also const).
For the decision to return a value:
1 if the return value may appear to the left of the = sign, you can only return a non-const reference as a left value.
2 If the return value can only appear to the right of the = number, the const reference or const value is returned only as a right value.
3 If the return value can appear either to the left or to the right of the = sign, the return value must be a left value, and a non-const reference is returned.
Operator overloading for example:
+ and-Overloading of operators:[CPP] View plain copy class point { private: int x; public: point (int x1) { x=x1;} point (point& p) { x=p.x;} const point operator+ (const point& p); Overloading the plus operator with member functions friend const point operator-(const point &&NBSP;P1,CONST&NBSP;POINT&&NBSP;P2)//Use the friend function to overload the minus operator }; const point point::operator+ (const point& p) { return point (x+p.x); } Point const operator-(CONST&NBSP;POINT&&NBSP;P1,CONST&NBSP;POINT&&NBSP;P2) { return point (p1.x-p2.x); }
Call: [CPP] view Plain copy point A (1); Point B (2); A+b; Correct, call member function a-b; correctly, the a+1 function of the ufida is adjusted; Correct, call the type conversion function first, turn 1 into object, then call member function A-1; Correct, call the type conversion function first, turn 1 into object, then adjust the function of 1+a; Error, when calling a member function, the first operand must be an object because the first operand also has the function to invoke the member function 1-a; Correct, first type conversion and then tune the function of the Ufida
Summarize:
1, because +-all appear on the right side of the = number, such as C=a+b, will return a right value, you can return the const type value
2, the latter several expressions discussed is, the number and the object mixed operator's situation, generally appears this kind of situation, often makes the Ufida function
3, the overload of the binocular Operator:
Overloaded operator function name: operator@ (Parameter table)
Implicit invocation form: OBJ1+OBJ2
Explicit invocation form: obj1.operator+ (OBJ obj2)---member function
operator+ (OBJ obj1,obj obj2)---friend function
When executed, the implicit invocation form and the explicit invocation form call the function operator+ ()
+ + and--overloading of operators: [CPP] view Plain Copy class Point {Private:int x; Public:point (int x1) {x=x1;} Point operator++ ()//member function defines the self-add const point operator++ (int x);