I. Why use Operator Overloading?
For all operators of the system, generally, only the basic data types and classes provided in the standard library are supported. for user-defined classes, if you want to support basic operations, such as comparing the size, to determine whether the operators are equal, and so on, you need to define the specific implementation of this operator by yourself. For example, to determine whether two people are equally large, the default rule is to compare them by age. Therefore, when designing the class "person", we need to consider the operator =, and, according to the analysis just now, the comparison is based on age. So why is it heavy load? This is because the implementation version of the basic data type of this operator has been provided for us during the implementation of the compiler, but now its operand has become a user-defined data type class, so, you must provide the implementation of this parameter version by yourself.
2. How to declare an overloaded operator?
A: Operator Overloading is implemented as a class member function.
An overloaded operator is declared in the class body. The declaration method is the same as a common member function, except that its name contains the keyword operator and a C ++ predefined operator followed by it.
You can declare a predefined = operator in the following way:
Class person {
PRIVATE:
Int age;
Public:
Person (int ){
This-> age =;
}
Inline bool operator = (const person & PS) const;
};
The implementation method is as follows:
Inline bool person: Operator = (const person & PS) const
{If (this-> age = ps. Age)
Return true;
Return false;
}
The call method is as follows:
# Include
Using namespace STD;
Int main ()
{Person p1 (10 );
Person P2 (20 );
If (p1 = P2) cout <"the age is equal !" <Return 0;
}
Here, because operator = is a member function of class person, objects P1 and P2 can call this function. In the above if statement, it is equivalent to P1 calling function =, p2 is passed to the function as a parameter of the function to achieve comparison between two objects.
Consider the following if statement:
If (10 = p1) cout <"the age is equal !" <Is the execution performed correctly?
The answer is no, because it is considered as a class member overload operator only when the left operand is an object of the class type. Because 10 is not a person object, you cannot call the classperson operator =.
Consider the following if statement:
If (person (10) = person (11 ))
Can cout <"OK" <be correctly executed? The answer is yes, because the operator is an anonymous object on both sides.
The overloaded operators do not require the two operands to be of the same type. For example, we can define a subscript operator for class person to indicate the correspondence between the person and the phone number:
/* Implement the subscript operator */
# Include
# Include
Using namespace STD;
Class person
{PRIVATE:
Int Tel;
Public:
Int & operator [] (string const & nm)
{
Return Tel;
}
Int gettel ()
{
Return Tel;
}
};
Int main ()
{
Person P1;
P1 [& quot; suo & quot;] = 110;
Person P2;
P2 [& quot; Rose & quot;] = 120;
Cout <return 0;
}
For operators that reload as member functions, the implicit this pointer is used as the first parameter of the function to represent the left operand.
B: Operator Overloading is implemented as a non-class member function (global function)
For global overload operators, parameters representing the left operand must be explicitly specified. For example:
# Include
# Include
Using namespace STD;
Class person
{
Public:
Int age;
Public:
};
/* The private data of this class cannot be accessed outside the class.
Set age to public */
Bool operator = (person const & P1, person const & p2)
{
If (p1.age = p2.age)
Return true;
Return false;
}
Int main ()
{
Person rose;
Person Jack;
Rose. Age = 18;
Jack. Age = 23;
If (Rose = Jack)/* two objects represent the Left and Right operands respectively */
Cout <"OK" <return 0;
}
C: How can I decide whether to reload an operator as a class member function or a global namespace member?
① If an overload operator is a class member, the operator will be called only when the left operand used with it is the object of this class. If the left operand of the operator must be of another type, the operator must be reloaded as a member of the global namespace.
② C ++ requires a value =, subscript [], call (), and the member pointing-> operator must be defined as a class member operator. Any definition that defines these operators as namespace members will be marked as a compilation error.
③ If an operand is a class type such as a string class, it is best to define a symmetric operator such as an equal operator as a global namespace member.
D: The operator overload is the method of the friend function.
If you reload the operator as a friend function, you can directly access the private data member of the authorization class within the function. This is the main difference between the method of friend function and the global namespace method.
E: How can I determine whether a non-class member operator should be a friend of the class or use a member to access the function? In general, the implementer of the class should try to minimize the number of operators represented inside the namespace function and the operator class. If you have provided access to member functions and they have the same efficiency, it is best to use these member functions. However, if the implementer of A Class decides not to provide access member functions for some private members of the class and the namespace operator must reference these private members to complete their operations, they must use the membership mechanism.
For example:
# Include
# Include
Using namespace STD;
Class person {
Public:
Int age;
Public:
};
Bool operator = (person const & P1, person const & p2)
{
If (p1.age = p2.age) return true;
Return false;
}
Ostream operator <(ostream & OS, person const & P)
{
OS <"the person age is:" <return OS;
}
Int main ()
{
Person rose;
Person Jack;
Rose. Age = 18;
Jack. Age = 23;
Cout </* Call ostream operator <(ostream & OS, person const & P )*/
Cout <return 0;
}
Iii. Design of overload Operators
Class 1 designers cannot declare a non-predefined overload operator.
② Other operators cannot be defined for built-in data types.
③ The predefined operator priority cannot be changed.
④ Which operators should be provided for a class are determined by the intended use of the class.
4. prefix and Postfix
To distinguish post-operator and pre-operator declarations, the declaration of the auto-increment and post-decimal operators for overload has an additional int type parameter. No parameter name is required because it is not used in operator definition. The extra integer parameter is transparent to users of the post operator, and the compiler provides it with the default value, so this parameter can also be ignored.
For example:
# Include
# Include
Using namespace STD;
Class person
{
PRIVATE:
Int age;
Public:
Person (int)
{
Age =;
}
Person const operator ++ ()/* prefix ++ */
{
This-> Age ++;
Return * this;
}
Person const operator ++ (int A)/* Postfix ++ */
{
Person temp (1 );
Temp = * this;
This-> Age ++;
Return temp;
}
Int getage ()
{
Return age;
}
};
Int main ()
{
Person rose (10 );
Person Jack (20 );
Person marry (22 );
Person Tom (30 );
Jack = ++ rose;
Marry = Tom ++;
Return 0;
}