Operator overloading and temporary objects

    In C++, operator overloading means redefining already defined operators with certain functions to complete more detailed and specific operations and other functions. There are many built-in data types in C++, such as char, int, float, etc., and each type has many operators, such as addition, subtraction, multiplication, division, etc. When the user defines an object of a class, these operators cannot be used directly between objects, such as the addition of complex numbers. Without overloading the + operator, the compiler would not know to add the real and real parts of complex numbers, and the imaginary and imaginary parts of complex numbers. This is where operator overloading comes in.

    There are three forms of operator overloading, one is overloaded as a member function of the class (this), one is overloaded as a non-member function of the class (without this), and the other is overloaded as a friend function of the class.

    The general syntax for operator overloading as a member function of a class is:

function type operator operator (parameter list)
{
  function body;
}

    The general syntax for operator overloading as a friend function of a class is:

friend function type operator operator (parameter list)
{
  function body;
}

    When the operator is overloaded as a member function of the class , the parameter of the operator overload will be one less than that of the operator function that is a friend or independent of the class, because the operator overloaded class member function will use the object that calls the function as the first parameter of the function. The parameter, that is, the implicit this pointer points to the first object that calls the function, so there will be one less parameter. Let's look at such an example:


    The leftmost object when calling an operator overloaded function defined in a class is the object when calling the operator overloaded function. For example, the += operator complex::operator += (const complex& r) { } (only one parameter, this is a hidden parameter) redefined in the class complex, and the objects c2 and c1 of the class complex call the operator overloading function The methods are c2 += c1 and c2.operator += c1, the former statement will be automatically converted to the latter statement, and the leftmost object c2 in the expression of c2 += c1 is the object that calls the operator overloaded function, and the right The c1 will be passed as parameter.

    In addition, we note that in the above example, the return value type of the function is a reference, and the sender does not need to know that the receiver is received in the form of reference.

    When a function is overloaded as a non-member function , let's look at the following example:


        We noticed that the complex( ) returned in the function body is a temporary object (temp object), these functions must not return by reference, because they must return a local object. = (assignment operator), () (function call operator), [ ] (subscript operator), -> (operator for accessing class members through pointers) These four operators can only be overloaded through member functions , The remaining operators can be overloaded by member or non-member functions.

    Restrictions on overloading:

    (1) Not all operators can be overloaded. Except ., .*, ::, ?:, sizeof, typeid, which cannot be overloaded, other operators can be overloaded;

    (2) Overloading cannot change the meaning of the operator when used for built-in types, and programmers cannot change the meaning of the + operator when used for two int types;

    (3) At least one parameter of the operator function must be an object of a class or a reference to an object of the class, which prevents programmers from using operators to change functions of built-in types;

    (4) Overloading cannot change the precedence of operators;

    (5) Overloading cannot change the associativity of operators;

    (6) Overloading cannot change the number of operator operands. For example, + requires two operands, and overloaded + must also have two operands.

Guess you like

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