1. Definition
Overloaded operations are functions with special names. Their names are composed of the keyword operator and the operation symbols to be defined later.
2. Operator table
2.1 Overloadable operators
2.2 List of non-overloadable operators
Operator | meaning |
---|---|
. | Member access operator |
., -> | Member pointer access operator |
:: | Domain operator |
sizeof | Length operator |
?: | Conditional operator |
# | Preprocessing symbol |
3. Design rules for overloaded operators
Rule 1
- The meaning of operators of built-in types cannot be changed.
- You can only overload existing operators, but cannot invent new operators.
- The precedence and associativity of overloaded operators are consistent with the corresponding built-in operators.
Rule 2
- Under normal circumstances, you should not overload the comma,, take address &, logical AND && and logical OR || operator.
- If the class contains arithmetic operators or bitwise operators, it is best to provide the corresponding compound assignment operators.
- How to have ==, generally there will be!=
- If there is a single order comparison, there should be operator<, and there should also be> and others.
- Logical operators return bool, arithmetic operators return class types, assignment operators and compound assignment operators return a reference to the operand on the left
4. Parameters of overloaded operators
The parameter represents the operand of the operator. For a binary operator, the left operand is passed to the first parameter, and the right operand is passed to the second parameter.
eg.
data1 + data2
operator+(data1, data2);
data1 += data2; // expression-based ''call''
data1.operator+=(data2); // equivalent call to a member operator function
- Except for the overloaded function call operator operator(), other overloaded operators cannot contain default arguments.
- The number of parameters of an overloaded operator function is as many as the number of operands of the operator.
- The number of explicit parameters of the member operator function is one less than the number of operands.
5. Design as a member function or a normal function
- When an operator is defined as a member function, the operand on its left side must be an object of the type that the operator belongs to.
- If an operator function is a member function of a class, its first operand will be bound to the implicit this pointer.
string s = "world";
string t = s + "!"; // ok: we can add a const char* to a string
string u = "hi" + s; // would be an error if + were a member of string
How to choose whether to define an operator as a member function or an ordinary function:
- Assignment =, subscript [], call () and member access arrow -> operator must be a member function.
- Compound assignment operators are generally member functions, but they are not required.
- Operators that change the state of an object or are closely related to a given type, such as increment, decrement, and dereference operators, usually member functions.
- Operators with symmetry may convert operands at either end, such as arithmetic, equality, relational, and bitwise operators, which are usually ordinary functions.