14.1 definition of overload Operators
The overload operator is a function with a special name: The operator symbol to be defined after the reserved word operator. Like any other function, the overload operator has a return type and a form parameter table.
In addition to the function call operator, the number of parameters of the overload operator (including the implicit this pointer of the member function) is the same as the number of operations of the operator. Function call operators can accept any number of operands.
2. the overload operator must have a class type operand.
It is used for built-in type operators and its meaning cannot be changed.
You cannot define additional operators for any built-in types.
The overload operator must have at least one class type or enumeration type operand. This rule forces the overload operator to not redefine the meaning of the built-in type object operator.
3. The priority and combination are fixed.
The operator priority, associativity, or number of operands cannot be changed.
In addition to the function call operator (), it is invalid to use the default real parameter when the operator is overloaded.
4. The short-circuit evaluation feature is no longer available
5. Most overload operators can be defined as common non-member functions or member functions of classes.
As a class member overload function, its form parameter seems to be less than the number of operands 1. As a member function operator, there is an implicit this form parameter, limited to the first operand.
Generally, arithmetic and Relational operators are defined as non-member functions, while value assignment operators are defined as members.
6. Operator overload and friend relationship
When operators are defined as non-member functions, they must be set to the friends of the operation class. In this case, the operator usually needs the private part of the callback class.
7. Use overload Operators
You can call the overload operator function just like calling a common function, specify the function, and pass the appropriate type of parameters.
Design of overload Operators
1. Do not reload operators with built-in meanings.
The value assignment operator, address fetch operator, and comma operator have default meanings for class type operands. If no specific version of the overload is available, the compiler defines these operators by itself.
You can change the meaning of these operators by redefining the operators of a given class type.
2. Most operators are meaningless to class objects.
Unless overload definitions are provided, the copy, fetch address, and comma operators are meaningless for class type operands. When designing a class, be sure to support those operators.
The best way to design operators for classes is to first design public interfaces for classes. After defining an interface, you can consider defining those operators as overload operators. Operations that can be logically mapped to an operator can be considered as candidate overload operators.
3. Compound value assignment operator
When the meaning of an overload operator is unknown, it is better to give the operation a name. Naming functions are usually better than operators for rarely used operations. If it is not a common operation, there is no need to use operators for conciseness.
4. Equal and Relational operators
The class to be used as the associated container key type should define the <operator. The associated container uses the <operator of the key type by default. Even if this type is stored only in an ordered container, classes should usually define equal (=) operators and smaller than (<) operators, on the grounds that many algorithms assume that these operators exist. For example, the sort algorithm uses the <operator, while the find algorithm uses the = Operator.
If the class definition class equals operator, it should also define unequal operators! =. If the class defines <, it may define all four Relational operators (>,>=, <,< = ).
5. select members or non-members
Assign values (=), subscript ([]), call (), and member access arrow (->) must be defined as members, defining these operations as non-member functions will be recorded as errors during compilation.
Like assignment, compound assignment operations should generally be defined as class members. Unlike assignment, it is not necessary to do so. If a non-member compound assignment operator is defined, no compilation error will occur.
Operators that change the object state or closely associate with a given type, such as auto-increment, auto-increment, and reference, should generally be defined as class members.
Symmetric operators are best defined as common non-member functions.
From xufei96's column