Operating instructions for overloaded operators (1)

An overloaded operator has a function with a special name:
the name of an overloaded operator consists of the keyword operator followed by the operator to be defined .
An overloaded operator also includes a return type , parameter list , and function body . An overloaded operator function takes as many
arguments as the number of operands on which the operator acts . For example, a unary operator takes one argument and a binary operator takes two arguments. Note ·: Except for the overloaded function call operator operator(), other overloaded operators cannot have default parameters.

The reason for the number of (explicit) arguments to a member operator function is one less than the total number of operands of the operator is that the
first left-hand operand is bound to the implicit this pointer.
When an operator acts on an operand of a built-in type, we cannot change the meaning of the operator
Example:
int operator+(int, int);//error, cannot redefine built-in operators for int
Summary: only existing ones can be overloaded The operator of , does not have permission to publish new operators.
±*& is both unary and binary operators, all of which can be overloaded.

The following is a summary of whether some operators can be overloaded:
insert image description here
Using an overloaded operator is essentially a function call,
so these rules about the order of evaluation of operands cannot be applied to overloaded operators. Generally, the comma operator
is not overloaded. And take the address operator **** Because these two operators already have built-in meanings, if they are overloaded, their behavior will be different from the normal, which will cause users of the class to be unable to adapt. Use overloaded operators judiciously Operator overloading Each operator has a more specific meaning when used with built-in types. Operator overloading works best when there is a logical mapping between the built-in operators and our own operations. Excessive use of operator overloading can also make our classes hard to understand. For example, it is very unfriendly to define operator+ and let it perform subtraction. Use operators only when the meaning of the operation is clear to the user. Using such an operator creates ambiguity if the user may have several different interpretations of the operator.









Assignment and compound assignment operators The assignment operator behaves like the compound version

: After assignment, the values ​​of the left operand and right operand are equal, and the operator should return a reference to its left operand.
Overloaded assignment operators should inherit rather than violate the meaning of their built-in versions.
If the class contains arithmetic operators it is best to provide corresponding compound assignment operators as well.
When we define an operator, we must first determine whether the member function is an ordinary non-member function, and decide whether to declare it as a member function of the class or as an ordinary non-member function.

The following guidelines help us decide whether to define an operator as a member function or a normal non-member function:

● Assignment (=), subscript ([]), call (l), and member access arrow (->) operators must be members. Unlike assignment operators, compound assignment operators generally should be members, but they don't have to be.
●Operators that change the state of an object or are closely related to a given type, such as increment, pass, and dereference operators, should usually be members.
●Operators with symmetry may convert the operands at either end, such as arithmetic, equality, relational and bit operators, etc., so they should usually be ordinary non-member functions.

Guess you like

Origin blog.csdn.net/2202_75623950/article/details/129478029