Algorithm training supplements C++ knowledge points: operator overloading

Supplement C++ knowledge points: operator overloading
  1. When I overloaded the operator, I didn't understand why I did it, and sometimes I made a mistake, so I added some knowledge points.

  2. Introduction:

    Operator overloading is just a "syntactic convenience", which means that it is just another way of function calling. The difference is that the parameters of the function do not appear in parentheses, but close to some characters. These characters are generally regarded as immutable operators.

    There are two differences between the use of operators and ordinary function calls. The first is the grammatical difference. When "calling" an operator, put the operator between the parameters, and sometimes after the parameters. The second difference is that the compiler decides which "function" to call. For example, if the + operator is used for a floating-point parameter, the compiler may "call" a function that performs addition of the floating-point type (this kind of call is usually inserted in inline code, or a piece of floating-point processor instruction).

    In C++, you can define a new operator for processing classes. This definition is very similar to the definition of an ordinary function, except that the name of the function consists of the keyword operator followed by the operator. The difference is only that. It is a function like other functions, and the compiler will call this function when it encounters an appropriate pattern.

  3. grammar:

    Defining an overloaded operator is like defining a function, except that the name of the function is operator@, where @ represents the operator being overloaded. The number of parameters in a function parameter depends on two factors:

    1. Whether the operator is unary (one parameter) or binary (two parameters).
      1. The operator is defined as a global function (one parameter for unary, two parameters for binary), or member function (no parameter for unary, and one parameter for binary—at this time, the object of this class uses the left parameter ).
      2. Several examples of operator overloading:
    //一元运算符重载
    const Integer& operator-(const Integer& a){
          
          //全局函数的形式
        return Integer(-a.i);
    }
    const Byte operator-() const {
          
          //成员函数的形式,写在一个对象的内部,作为成员方法
        return Byte(-this.b);
    }
    const Integer& operator++(Integer &a){
          
          //全局形式,实现++a;
        a.i++;
        return a;
    }
    const Integet& operator++(Integer &a, int){
          
          //全局形式实现a++;
        Integer before(a.i);
        a.i++;
        return before;
    }
    //二元运算符重载
    int operator<(const Integer& left, const Integer& right){
          
          //全局形式
    			return left.i < right.i;
    }
    int operatot<(const Byte& right)const{
          
          //成员方法形式,放在一个类中
        return this.b < right.b;
    }
    Byte& operator=(const Byte& right){
          
           // 对赋值运算符的重载,其只能作为成员方法的形式进行重载。
        if(this == &right)return *this;
        this.b = right.b;
        return *this;
    }
    Integer& operator+=(Integer& left, const Integer& right){
          
          //注意对左值是非常量引用
        left.i += right.i;
        return left;
    }
    
    1. Parameters and return values:

      In the above several examples, we have seen different parameter passing and returning methods, they follow a pattern of protecting logic, we should choose this pattern in most cases:

      1. For any function parameter, if you only need to read from the parameter without changing it, it should be passed by reference as const by default. For example, ordinary operators and non-dual operators do not change the parameters themselves, so const is used to pass by reference, while increment and decrement will change the parameters themselves and cannot be passed by const.
      2. The parameter type of the return value depends on the specific meaning of the operation symbol (we can do whatever we want with the parameters and return value).
      3. All assignment operators change the lvalue, so for convenience, the return value of all assignment operators should be a non-constant reference to the lvalue.
      4. For logical operators, people want at least an int return value, preferably a bool return value.

Guess you like

Origin blog.csdn.net/qq_40596572/article/details/104880538