Detailed rules for C++ overloaded operators

  • 2020-04-02 01:50:44
  • OfStack

(1) C++ does not allow users to define new operators, and can only overload existing C++ operators.
For example, some people find it convenient to use "* *" as a power operator in BASIC, but they also want to define "* *" as a power operator in C++ and use "3* *5" for 35, which is not possible.

(2) operator allowed to be overloaded in C++
Most operators in C++ can be overridden.

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 overridden to ensure that the function of accessing members cannot be changed. The operand object that conforms to the sizeof operator of the field operation is a type rather than a variable or general expression and does not have the characteristic of overloading.

(3) overloading cannot change the number of operator objects (that is, operands).
For example, the relational operator" > "And" < "And so is a binocular operator, after overloading is still a binocular operator, need two parameters. The operators "+", "-", "*", "&", etc., can be either monocular or binocular operators, and can be overridden as either a monocular or a binocular operator, respectively.

(4) overloading cannot change the priority level of the operator.
For example, "*" and "/" have higher priority than "+" and "-", the priority between the operators does not change no matter how overloaded. Sometimes you want to change the priority of an operator in a program, and you can only use a parenthesized method to force a change in the order in which overloaded operators operate.

(5) overloading cannot change the associativity of operators.
For example, the copy operator "=" is right associative (from right to left) and remains right associative after overloading.

(6) functions with overloaded operators cannot have default arguments
Otherwise, the number of operator parameters is changed, contradicting the previous point (3).

(7) an overloaded operator must be used with a user-defined object of a custom type, and at least one of its arguments should be a class object (or a reference to a class object).
That is, parameters cannot all be standard types of C++ to prevent users from modifying the properties of the operators used for data members of standard types, such as the following:


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

The operator +, which was used to add two Numbers, is now being overloaded to subtract two Numbers.

So if you're allowed to overload like this, if you have an expression 4 plus 3, does it give you a 7 or a 1? Obviously, this is absolutely forbidden.

(8) operators for class objects generally must be overridden, with two exceptions, the operators "=" and "operator" & "do not have to be overridden by the user.
The copy operator "=" can be used for each class object, and can be used to assign values to each other. Because the system has overloaded an assignment operator for each newly declared class, it copies the data members of the class one by one

The address operator & also does not have to be overloaded; it returns the starting address of the class object in memory.

(9) the function of the overloaded operator should be similar to what is implemented when the operator is applied to data of standard types.
For example, we overload "+" to add objects, but we don't overload "+" to subtract objects, because that's not what we thought "+" would be.

(10) operator overload function can be a member function of a class, a friend function of a class, or an ordinary function that is neither a member function of a class nor a friend function.


Related articles: