为什么在此代码中不调用复制构造函数 [英] Why is copy constructor not being called in this code

查看:121
本文介绍了为什么在此代码中不调用复制构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

那么为什么不在 const Integer operator +(const Integer& rv)函数中调用Copy构造函数。是因为RVO。 如果是,我需要做什么来防止它?

  #include< iostream& 

using namespace std;

class Integer {
int i;

public:
Integer(int ii = 0):i(ii){
cout< Integer()<< endl;
}

Integer(const Integer& I){
cout< Integer(const Integer&)<< endl;
}

〜Integer(){
cout< 〜Integer()<< endl;
}

const整数运算符+(const Integer& rv)const {
cout< operator +<< endl;
Integer I(i + rv.i);
I.print();
return I;
}

Integer& operator + =(const Integer& rv){
cout< operator + =<< endl;
i + rv.i;
return * this;
}

void print(){
cout< i:< i<< endl;
}
};

int main(){
cout<< 内置tpes:< endl;
int i = 1,j = 2,k = 3;
k + = i + j;
cout<< user-defined types:<< endl;
Integer ii(1),jj(2),kk(3);
kk + = ii + jj;

}



我会收到错误构造函数。我期待复制构造函数,当operator +返回时被调用。以下是程序的输出

 内置tpes:
用户定义的类型:
Integer ()
Integer()
Integer()
operator +
Integer()
i:3 //预期复制构造函数在此之后调用
operator +
〜Integer()
〜Integer()
〜Integer()
〜Integer()

解决方案


是因为RVO。如果是,我需要做什么来防止它?


是的。但它没有被调用,因为编译器返回值优化



如果您使用GCC,请使用 -fno-elide-constructors 选项来避免它。



GCC 4.6.1手册说,


-fno-elide-constructors / p>

C ++标准允许实现省略创建只用于初始化同一类型的另一个对象的临时。指定此选项将禁用该优化,并强制G ++在所有情况下调用复制构造函数。



So why is Copy constructor not being invoked in "const Integer operator+(const Integer &rv)" function. Is it because of RVO. If Yes what do I need to do to prevent it?

#include <iostream>

using namespace std;

class Integer {
    int i;

public:
    Integer(int ii = 0) : i(ii) {
        cout << "Integer()" << endl;
    }

    Integer(const Integer &I) {
        cout << "Integer(const Integer &)" << endl;
    }

    ~Integer() {
        cout << "~Integer()" << endl;
    }

    const Integer operator+(const Integer &rv) const {
        cout << "operator+" << endl;
        Integer I(i + rv.i);
        I.print();
        return I;
    }

    Integer &operator+=(const Integer &rv) {
        cout << "operator+=" << endl;
        i + rv.i;
        return *this;
    }

    void print() {
        cout << "i: " << i << endl;
    }
};

int main() {
    cout << "built-in tpes:" << endl;
    int i = 1, j = 2, k = 3;
    k += i + j;
    cout << "user-defined types:" << endl;
    Integer ii(1), jj(2), kk(3);
    kk += ii + jj;

}

I do get an error If I'll comment out copy constructor. I'm expecting copy constructor to be called when operator+ returns. Following is the output of the program

built-in tpes:
user-defined types:
Integer()
Integer()
Integer()
operator+
Integer()
i: 3 // EXPECTING Copy Constructor to be called after this
operator+=
~Integer()
~Integer()
~Integer()
~Integer()

解决方案

Is it because of RVO. If Yes what do I need to do to prevent it?

Yes. But it didn't get called because of Return Value Optimization by the compiler.

If you're using GCC, then use -fno-elide-constructors option to avoid it.

GCC 4.6.1 manual says,

-fno-elide-constructors

The C++ standard allows an implementation to omit creating a temporary which is only used to initialize another object of the same type. Specifying this option disables that optimization, and forces G++ to call the copy constructor in all cases.

这篇关于为什么在此代码中不调用复制构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆