C + + allows overloaded operators and operators that do not allow overloading
Most operators in C + + allow overloading, as specified in the table
There are only 5 operators that cannot be overloaded:
. (Member access operator)
. * (Member pointer access operator)
:: (field operator)
sizeof (length operator)
?: (conditional operator)
The first two operators cannot be overloaded to ensure that the functionality of the access member cannot be changed, and that the field and sizeof operators are types rather than variables or generic expressions, and do not have overloaded features.
Rules for C + + operator overloading
C + + defines the following several rules for operator overloading.
1 C + + does not allow users to define new operators themselves, only the existing C + + operators can be overloaded. For example, some people think that basic "* *" as a power operator is convenient, but also want in C + + to define "* *" as a power operator, "3**5" to represent 35, this is not the case.
2 The overload cannot change the number of operator operations objects (that is, shoving count). For example, the relational operator ">" and "<" are binocular operators, which are still binocular operators after overloading, requiring two parameters. Operators "+", "-", "*", "&" can be used as either monocular operators or as binocular operators, and can be overloaded individually as a single or binocular operator.
3 overload cannot change the precedence of an operator. For example, "*" and "/" precedence over "+" and "-", regardless of how the overload, the precedence between the operators will not change. Sometimes you want to change the precedence of an operator in a program, and you can only enforce the order of operations for overloaded operators by using parentheses.
4 overload cannot change the binding of operators. If the assignment operator is right-bound (from right to left), the overload is still the right binding.
5 The function of the overloaded operator cannot have a default parameter, otherwise the number of operator arguments is changed, which contradicts the preceding (2) point.
6 An overloaded operator must be used with an object of a user-defined custom type, and its arguments should have at least one class object (or a reference to a class object). That is, parameters cannot be all standard types of C + + to prevent users from modifying the nature of the operators used for standard type data, as the following is wrong:
int operator + (int a,int b)
{
retum (a-b);
}
The effect of the original operator + is to add two numbers, and now attempts to subtract the effect by overloading it with two digits. If this overload is allowed, if there is an expression 4+3, does it result in 7 or 1? Obviously, this is absolutely forbidden.
If you have two parameters, both of these parameters can be class objects, or a class object, and a C + + standard type of data, such as
Complex operator + (int a,complex&c)
{return
Complex (a +c.real, c.imag);
}
Its function is to make an integer and a complex number added together.
7 operators used for class objects generally must be overloaded, but with two exceptions, the operator "=" and "&" do not have to be overloaded.
The ① assignment operator (=) can be used for each class object, which can be used to assign values to one another in a homogeneous object. We know that you can use the assignment operator to assign a value to a class, because the system has overloaded an assignment operator for each newly declared class, and its role is to copy the data members of the class one after another. The user can assume that it is the default object assignment operator provided by the system and can be used directly for assignment between objects without overloading itself. However, sometimes the default object assignment operator provided by the system does not meet the requirements of the program, for example, when a data member contains a pointer member that points to dynamically allocating memory, the risk may occur when replicating this member. In this case, you need to overload the assignment operator yourself.
The ② address operator & does not have to be overloaded, it can return the starting address of the class object in memory.
8 Theoretically, an operator can be overloaded to perform arbitrary operations, such as overloading the addition operator with the information in the output object, and overloading the ">" operator with the "less than" operation. But this violates the original intention of operator overloading, instead of mentioning Gao readability, it makes people inexplicable, unable to understand the program. Overloaded operators should be made similar to the functionality that is implemented when the operator acts on standard type data (such as adding with "+", and ">" Implementing "Greater than" relational operations).
9 operator overloading function can be a member function of a class, or it can be a friend function of a class, or it can be a normal function that is neither a member function of a class nor a friend letter.
These rules are easy to understand and do not have to be rote. The introduction of them together, just to make the reader have a holistic concept, but also easy to access.