0

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?

Karthikgr
  • 81
  • 13
  • Have you looked at https://stackoverflow.com/questions/4421706/what-are-the-basic-rules-and-idioms-for-operator-overloading? – R Sahu Apr 07 '20 at 04:16
  • You shouldn't do anything other than copy your members in your copy constructor and copy assignment operator. If your code is doing more than that, you should probably rethink it. – Rish Apr 07 '20 at 04:20
  • @Karthikgr When you create an assignment operator or copy constructor, do not make them stub functions as you did now -- these are not simply throw-away functions that do not matter. Once you create such functions, you *must* assign the members correctly. Right now, your copy constructor and assignment operator do nothing but output a message, which leads to weird behavior occurring. – PaulMcKenzie Apr 07 '20 at 04:46
  • @PaulMcKenzie ya got it, but i just had a doubt and i wanted to express it in terms of code, so didnt assign anything there.It wasnt for any project, i was just trying to learn concepts. – Karthikgr Apr 07 '20 at 04:50
  • @Rish ya thanks man i got it . – Karthikgr Apr 07 '20 at 04:51
  • For your `operator+`, you can pass the argument by reference (to const, because the passed `loc` probably shouldn't be modified by this call) to avoid calling the copy-constructor : `loc loc::operator+ (const loc& op2)`. – melk Apr 07 '20 at 05:54

1 Answers1

1

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;
}
Karthikgr
  • 81
  • 13