The rules for overloading operators are as follows:
1. Except a few operators in C ++, they can all be reloaded, and only existing operators in C ++ can be reloaded.
2. After overloading, the operator's priority and associativity will not change.
3. Operator Overloading is an actual need for new types of data. The original operators should be transformed as appropriate. In general, the overloaded functions should be similar to the original functions, the number of operation objects of the original operator cannot be changed. At least one operation object must be of the custom type.
Note: There are only five operators that cannot be overloaded. They are class Relational operators ". ", member pointer operator". * ", scope identifier": ", sizeof operator, and three-wood operator"? :". The preceding two operators ensure that the meaning of the access member function in C ++ is not changed. The operands of the range delimiters and sizeof operators are of type.
4. There are two methods to overload operators: Reload is a member function of the class and overload is a friend function of the class.
- The general syntax format of a member function whose operator is overloaded as a class is:
Operator of function type (parameter table)
{
Function body
}
- Operator Overloading is a class-based friend function. You can declare the prototype of the friend function in the class, implement it outside the class, or implement it in the class body.