Karthikgr
Karthikgr

Reputation: 81

Operator overloading affects my copy constructor

I was learning operator overloading and i encountered a scenario where i need a clear understanding or an alternative solution. Here is my code.

#include<iostream>

class loc {
    int longitude, latitude;
public:
    loc() {
        longitude = 0;
        latitude = 0;
        int i = 0;
        std::cout << "Constructor Called " << i++ << std::endl;
    }
    loc(int lg,int lt) {
        longitude = lg;
        latitude = lt;
    }
    loc(loc const& op2) {
        std::cout << "Copy Constructor Called " << std::endl;
    }
    void operator= (loc op2){
        std::cout << "Assignment operator called " << std::endl;
    }
    void show() {
        std::cout << longitude << " " << latitude << std::endl;
    }
    loc operator+ (loc op2);
};

loc loc::operator+ (loc op2) {
    loc temp;
    temp.longitude = this->longitude + op2.longitude;
    temp.latitude = latitude + op2.latitude;
    return temp;
}


int main() {

    loc ob1(5,6), ob2(10,15);
    ob1.show();
    ob2.show();

    ob1 = ob1 + ob2;
    ob1.show();
    return 0;
}

This will be the output of my program:

5 6
10 15
Copy constructor called
Constructor called 0
Copy constructor called
Assignment operator called
5 6

In the above code when operator + is overloaded as per operator overloading rules, the object to the right of the operator is passed to the operator+ function as pass by value.

Thus this makes my copy constructor to invoke, here in the above code i haven't added any logic in my copy constructor function so it doesn't affects my code, but what if some logic is present in this and that affects my code? How should i avoid invoking of copy constructor? When i return my temporary object from the operator+ function then also copy constructor is invoked!

I have also declared Assignment operator which will also be called and thus my result wont be displayed as 15 21 , but displays the value of ob1's before addition value.

Is this how it will work and we have to handle all this scenarios or is it anything that can be changed in my code or in the way of approach?

I came across a similar question in stack overflow and here is the link for it: Copy Constructor And Operator Overloading +

All i want know is there something i can address in my code and improve OR this is how it works and everything should be taken care of? How to avoid this confusion if i had to use all of these copy constructor and operator overloading?

Upvotes: 0

Views: 260

Answers (1)

Karthikgr
Karthikgr

Reputation: 81

As per question copy constructor should not be invoked through operator overloading and hence reference type has to be passed as an argument and returned with reference type instead of pass by value. Thus it doesn't create objects and copy constructor is never called.

loc& loc::operator+ (loc& op2) {
    longitude = this->longitude + op2.longitude;
    latitude = latitude + op2.latitude;
    return *this;
}

Upvotes: 1

Related Questions