C++ operator overloading

The operation objects of predefined operators in C++ can only be basic data types. In fact, for many user-defined types, similar operations are also required. E.g:

class complex
{
public:
    complex(double r=0.0,double I=0.0) {
        real=r;
        imag=I;
    }
    void display();
private:
    double real;
    double imag;
};
complex a(10,20),b(5,8);

How is the "a+b" operation implemented? At this time, we need to write our own program to explain what function "+" should achieve when it acts on the complex class object. This is operator overloading. Operator overloading is to assign multiple meanings to existing operators, so that the same operator acts on different types of data, resulting in different types of behavior.
The essence of operator overloading is function overloading. In the implementation process, the specified operation expression is first converted into a call to the operator function, the operand is converted into the actual parameter of the operator function, and then the required function to be called is determined according to the type of the actual parameter. This process loves the compilation process completed in.

1. The rules of
operator overloading The operator overloading rules are as follows:
(1) Except for a few operators in C++, all operators can be overloaded, and only the existing operators in C++ can be overloaded.
(2) The precedence and associativity of the operator will not change after overloading.
(3) Operator overloading is to appropriately transform the original operator according to the actual needs of the new type of data. In general, the overloaded function should be similar to the original function, the number of operands of the original operator cannot be changed, and at least one operand must be a custom type.
There are only five operators that cannot be overloaded, they are: member operator ".", pointer operator "*", scope operator "::", "sizeof", conditional operator "?:".
There are two forms of operator overloading, overloading as a member function of a class and overloading as a friend function of a class.
The general grammatical form of operator overloading as a member function of a class is:
function type operator operator (formal parameter list)
{   function body; } The general grammatical form of operator overloading as a friend function of a class is: friend function type operator (formal parameter list) parameter list) {   function body;






}
Among them, the function type is the result type of the operation; the operator is the keyword for defining the overloaded function of the operator; the operator is the name of the overloaded operator.
When the operator is overloaded as a member function of the class, the number of parameters of the function is one less than the original number of operations; when the operator is overloaded as a friend function of the class, the number of parameters is the same as the number of the original operands. The reason is that when an object is overloaded as a member function of a class, if an object uses the overloaded member function, its own data can be accessed directly, and it does not need to be passed in the parameter table. The less operand is the object. itself. When overloaded as a friend function, when the friend function operates on the data of an object, it must be done through the name of the object, so the parameters used must be passed, and the number of operands will not be Variety.
The main advantage of operator overloading is that it allows changing the way operators used within the system operate to accommodate similar operations on user-defined types.

2. Can be used as overloaded operators
Arithmetic operators: + , - , * , / , % , ++ , -- ;
Bit manipulation operators: & , | , ~ , ^ , < , > >
Logical operations Operators: ! , && , || ;
Comparison operators: < , > , >= , <= , == , != ;
Assignment operators: = , += , -= , *= , /= , %= , &= , |= , ^= , <<= , >>= ;
other operators: [] , () , -> , , (comma operator) , new , delete , new[] , delete[] , - >*.
The following operators are not allowed to overload:
. , .* , :: , ?:

3. After operator overloading, precedence and associativity
The user overloads the newly defined operator without changing the precedence and associativity of the original operator. That is to say, overloading the operator does not change the precedence and associativity of the operator, and after the operator is overloaded, it does not change the grammatical structure of the operator, that is, the unary operator can only be overloaded as a unary operator. operator can only overload binary operators.
 
4. How does the compiler choose which operator function
The operator overloading is actually a function, so the overloading of the operator is actually the overloading of the function. The compiler's selection of operator overloading follows the selection principle of function overloading. When encountering a non-obvious operation, the compiler will look for an operator function with matching arguments.
 
5. What are the limitations of overloaded operators
(1) Do not invent new operators. Overloaded operators must be restricted to those allowed within the scope of operators already in the C++ language.
(2) Overloaded operators insist on 4 "can't change".
·Cannot change the number of operands of the operator;
·Cannot change the original priority of the operator;
·Cannot change the original associativity of the operator;
·Cannot change the original syntax structure of the operator.
 
6. What principles must be followed when overloading
operators Operator overloading can make programs more concise, make expressions more intuitive, and increase readability. However, operator overloading should not be used too much, otherwise it will bring certain troubles.
(1) The meaning of overloaded operators must be clear.
(2) Overloaded operators cannot be ambiguous.
Two forms of
operator overloaded functions Operator overloaded functions generally take the following two forms: member function form and friend function form. Both forms can access private members in the class.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324499624&siteId=291194637