07 C + + operator overload

c + + operator overloading

1. Why overload operators?

Operator operands in c + + are limited to basic built-in data types, but there is no way to operate on custom types (classes). For example, the following operation: p1 + p2, which cannot be implemented without overloading: p1 + p2

class{
private:
    int x;
    int y;
public:
    ...
}

int main(int argc, char **argv)
{
    Point p1(1, 2);
    Point p2(2, 3);
    Point sum = p1 + p2;
}

2. The essence of c + + operator overloading

Operator is essentially a function. Since it is a function, it can be overloaded. To overload operators, you need to use the following format:

< return type specifier > operator < operator symbol > (< parameter table >)  
{  

     < function body >  

}  

3. Examples

3.1. No. + overload

class Point{
private:
    int x;
    int y;
public:
    ...
    friend Point operator+(Point &p1, Point &p2);
};

Point add(Point &p1, Point &p2)
{
    Point n;
    n.x = p1.x + p2.y
    n.y = p1.y + p2.y;
    return n;
}

Point operator+(Point &p1, Point &p2)
{
    cout<<"Point operator+(Point &p1, Point &p2)"<<endl;
    Point n;
    n.x = p1.x + p2.x;
    n.y = p1.y + p2.y;
    return n;
}

int main(int argc, char **argv)
{
    Point p1(1, 2);
    Point p2(2, 3);
    Point sum = p1 + p2;/* Direct call: operator+(p1, p2) is also possible */
    return 0;
}

3.2 overloading of pre - + + and post - + +

class Point{
private:
    int x;
    int y;
public:
    ...
    void printInfo()
    {
        cout<<"("<<x<<", "<<y<<")"<<endl;
    }

    friend Point operator++(Point &p);
    friend Point operator++(Point &p, int a);
};

Point operator++(Point &p)  /* Pre + + */
{
    cout<<"++p"<<endl;
    p.x += 1;
    p.y += 1;
    return p;
}

Point operator++(Point &p, int a) /* Post + + */
{
    cout<<"p++"<<endl;
    Point n;
    n = p;
    p.x += 1;
    p.y += 1;
    return n;
}

int main(int argc, char **argv)
{
    Point p1(1, 2);
    Point p2(2, 3);
    Point n = ++p1;
    n.printInfo();
    p1.printInfo();

    cout << "******************"<<endl;

    Point m = p2++;
    m.printInfo();
    p2.printInfo();
    return 0;
}

It is also possible to return a reference to an object before overloading + +, as follows:

Point &operator++(Point &p)
{
    cout<<"++p"<<endl;
    p.x += 1;
    p.y += 1;
    return p;
}

4. What is the difference in efficiency between returning an object and returning a reference to an object?

When an object is returned in a function, a temporary object will be created first, the temporary object will be constructed according to the return value type, the constructor will be called, and the destructor will be called after the object is used up, which consumes more execution time. This problem does not occur when a reference to an object is returned.

5. When to return an object, and when to return a reference to an object?

Return the reference without affecting the result to improve the execution efficiency of the program. For example: the first + + return reference is correct, and the second + + return reference will affect the results and errors.

Posted on Sun, 03 May 2020 06:12:52 -0400 by flientje