Home>

C++ allows overloaded operators and does not allow overloaded operators

Most operators in C++ allow overloading,See specific table

There are only 5 operators that cannot be overloaded:

. (Member access operator)

. * (Member pointer access operator)

::(domain operator)

sizeof (length operator)

? ;:(conditional operator)

The first two operators cannot be overloaded to ensure that the function of the access member cannot be changed.The domain and sizeof operators operate on types rather than variables or general expressions.Does not have the characteristics of overloading.

C++ operator overloading rules

C++ defines the following rules for operator overloading.

1) C++ does not allow users to define new operators by themselves,You can only overload existing C++ operators. For example, some people find it convenient to use "**" as a power operator in basic.I also want to define "**" as a power operator in C++,Use "3 ** 5" to indicate 35, which will not work.

2) Overloading cannot change the number of operators (ie, operands). Relational operators ">" and "<" are binocular operators,Binary operator after overloading,Requires two parameters.The operators "+", "-", "*", "&", etc. can be used as monocular operators,Can also be used as a binocular operator,They can be overloaded as monocular operators or binocular operators, respectively.

3) Overloading cannot change the precedence of operators.For example, "*" and "/" take precedence over "+" and "-". Regardless of the overload,The precedence level between the operators does not change.Sometimes i want to change the priority of an operator in a program,You can only use parentheses to force the order of operations of overloaded operators to change.

4) Overloading cannot change the inclusiveness of the operator.If the assignment operator is right-associative (from right to left), it is still right-associative after overloading.

5) Functions with overloaded operators cannot have default parameters,Otherwise, the number of operator parameters is changed,Contradiction with point (2) above.

6) Overloaded operators must be used with user-defined objects of a custom type,At least one of its parameters should be a class object (or a reference to a class object). That is,Parameters cannot all be standard types in C++,To prevent users from modifying the nature of the operators used for standard types of data,It's wrong like this:

 int operator + (int a, int b)
  {
    retum (a-b);
  }

The role of the + operator is to add two numbers together,Attempts are now made to reduce its effect to two numbers by overloading. If such overloading is allowed,If there is an expression 4 + 3, is the result 7 or 1?Obviously, this is absolutely forbidden.

If there are two parameters,Both parameters can be class objects,Can also be a class object,One is c++ standard type of data,Such as

 complex operator + (int a, complex&c)
  {
    return complex (a + c.real, c.imag);
  }

Its role is to add an integer and a complex number.

7) Operators used for class objects must generally be overloaded,There are two exceptions.The operators "=" and "&" need not be overloaded.

① The assignment operator (=) can be used for every class object,You can use it to assign values ​​to similar objects. we know,You can use assignment operators to assign values ​​to objects of a class,This is because the system has overloaded an assignment operator for each newly declared class,Its role is to copy the data members of the class one by one.Users can think of it as the default object assignment operator provided by the system,Can be used directly for assignment between objects,You don't have to do the overloading yourself.But sometimes the default object assignment operator provided by the system cannot meet the requirements of the program.For example, when a data member contains a pointer member to dynamically allocated memory,Danger may arise when copying this member.under these circumstances, 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,You can overload an operator to perform any operation,If you can overload the addition operator as information in the output object,Overload the ">" operator to a "less than" operation. But this violates the original intention of operator overloading,Instead of improving readability,But inexplicable,Can't understand the program.The function of the overloaded operator should be made similar to that implemented when the operator is applied to standard type data (such as adding with "+",Use ">" to implement "greater than" relational operations).

9) Operator overloaded functions can be member functions of a class,It can also be a friend function of a class,It can also be a normal function that is neither a member function of a class nor a friend function.

These rules are easy to understand,No need to rote.Put them all together,Just to give the reader a holistic concept,Also easy to consult.

c
  • Previous Make simple devour effects based on replaceChild
  • Next 360 degree rotation of product images based on jQuery Circlr plugin