An operator overload function can be declared as a member function of a class, or as a global function other than all classes.
Operator overloading functions as member functions of a class
When you declare an operator overload function as a member function of a class, there is only one argument for the two-tuple operator, and the unary operator does not require arguments. One parameter is missing because this parameter is implied.
For example, the addition operator is overloaded in the complex class of the previous section:
operator+ (const Complex & A)const;
When executing:
C3 = C1 + C2;
will be converted to:
C3 = C1. operator+ (C2);
The C1 member variable is accessed implicitly through the this pointer.
Operator overloading functions as member functions of a class
When you declare an operator overload function as a global function, the two-tuple operator requires two arguments, a unary operator requires a parameter, and one of the arguments must be an object, so that the compiler can differentiate it from the programmer's custom operator, preventing the programmer from modifying the nature of the operators used for built-in types.
For example, the following is not true:
int operator + (int A,int b) { return (A-b);}
The "+" number was originally added to two numbers, and now attempts to change its function to two number subtraction by overloading. If such overloading is allowed, will the result of the expression 4+3 be 7 or 1? Obviously, this is absolutely forbidden.
If there are two parameters, both of these parameters can be objects, one is an object, and the other is a C + + built-in type of data, such as:
operator+ (int A, complex &c) { return complex (A +c.real, C.imag );}
Its purpose is to make an integer and a complex number Add.
Change the complex class of the previous section, and use the global operator overload function to calculate the sum of the complex numbers:
#include <iostream>using namespacestd;classcomplex{Private: DoubleReal//Real Department DoubleImag//Imaginary Part Public: Complex (): Real (0.0), Imag (0.0) {} complex (DoubleADoubleb): Real (a), imag (b) {}voidDisplay ()Const{cout<<real<<" + "<<imag<<"I"<<Endl;} Friend Complexoperator+(ConstComplex &a,ConstComplex &b);//friend function};//Global operator OverloadingComplexoperator+(ConstComplex &a,ConstComplex &B) {Complex C; C.real= A.real +B.real; C.imag= A.imag +B.imag; returnC;}intMain () {Complex C1 (4.3,5.8); Complex C2 (2.4,3.7); Complex C3; C3= C1 +C2; C3.display (); return 0;}
Because overloaded operator functions use the private variables of the complex class, we declare the function as a friend function. When executing:
C3 = C1 + C2;
will be converted to:
operator+ (c1, C2);
Finally, two points are stated:
- Only rarely do you use a normal function that is neither a member function of a class nor a friend function, because the normal function does not directly access the private members of the class, as mentioned above.
- The pointer operator, the subscript operator, [], the function call operator (), and the assignment operator "=" can only be overloaded as member functions.
C + + Learning 27 using global function overloading operators