简体   繁体   English

运算符重载会影响我的复制构造函数

[英]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.在上面的代码中,当运算符+按照运算符重载规则进行重载时,运算符右侧的 object 作为按值传递传递给 operator+ function。

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?因此,这使我的复制构造函数被调用,在上面的代码中,我没有在我的复制构造函数 function 中添加任何逻辑,所以它不会影响我的代码,但是如果其中存在一些逻辑并且影响我的代码怎么办? How should i avoid invoking of copy constructor?我应该如何避免调用复制构造函数? When i return my temporary object from the operator+ function then also copy constructor is invoked!当我从 operator+ function 返回我的临时 object 时,也会调用复制构造函数!

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.我还声明了赋值运算符,它也将被调用,因此我的结果不会显示为 15 21,而是显示 ob1 在加法值之前的值。

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?如果我必须使用所有这些复制构造函数和运算符重载,如何避免这种混淆?

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM