Detailed explanation of operator overloading, and Operator Overloading
Normal operator overload
During operations between objects, the program calls the function corresponding to the operator for processing. Therefore, there are two methods to overload the operator:Member functions and friend functions.The form of member functions is relatively simple, that is, a function related to operators is defined in the class. The membership function does not have the this pointer, so there will be one more parameter.
// Operator overload, which is also called the value assignment function string & operator = (const string & other); // operator overload, but here we need to use the friend meta function for friend ostream & operator <(ostream & OS, const string & str); string & string: operator = (const string & other) {if (this = & other) return * this; delete [] m_data; m_data = NULL; m_data = new char [strlen (other. m_data) + 1]; strcpy (m_data, other. m_data); return * this;} ostream & operator <(ostream & OS, const string & str) // return reference is used to implement the chained expression {OS <str. m_data; return OS ;}
Differences between member function overloading and feature overloading:
If you define your operator overloaded function as member function, thenCompiler translates expressions likes1 + s2Intos1.operator+(s2). That means, the operator overloaded member function gets invoked on the first operand.That is how member functions work!
But what if the first operand is not a class?There's a major problem if we want to overload an operator where the first operand is not a class type, rather saydouble.So you cannot write like this10.0 + s2. However, you can write operator overloaded member function for expressions likes1 + 10.0.
To solve thisOrderingProblem, we define operator overloaded functionfriendIF it needs to accessprivateMembers. Make itfriendONLY when it needs to access private members. Otherwise simply make itNon-friend non-memberFunctionImproveEncapsulation!
Comparison of the two overload methods:
- In general, 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.
- The following binary operators cannot be used as class-related friend functions: =, (), [],->.
- A type conversion function can only be defined as a member function of a class, but not a friend function of a class. C ++ provides four types of conversion functions: reinterpret_cast (conversion during compilation), const_cast (conversion during compilation), and stactic_cast (conversion during compilation), dynamic_cast (realize conversion during running, and can return the sign that the conversion is successful or not ).
- If an operator operation needs to modify the object state, it is better to select reload as a member function.
- If the required operand (especially the first operand) needs to have implicit type conversion, you can only use the youyuan function.
- 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 type, this operator function must be implemented as a friend function. (For example, the stream operator <,>)
- When you need to overload operators with interchangeable, select the overload as a friend function.
Note: