复制未调用的构造函数? [英] Copy constructor not called?

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

问题描述

200新X- 200 200 X- 200 200 -40 -40 200 200 -40 200 200 -40 200 200 -40 200 -40 200 200 200 200:新新旗新新新旗新新新旗新新旗新新旗新新旗新200旗新新旗新200旗新200新新旗新新旗新200旗新新旗新200旗旗新新旗旗新新旗新200旗新新旗新200旗新新旗旗新200旗新新旗旗新200旗旗新新200新新旗200新新旗新新200新旗新新旗200新新新200新新旗200新新新旗新新旗200新新新旗新新旗200新新新旗新新旗新200新旗新新旗新200新旗新新旗旗规规200 200醒醒新旗新200新新旗新200新旗新新旗新200新旗新新旗新200新旗新新旗新200新旗新新旗新200新新旗新新旗2001-新评新200 200 200 200 200 200 200 200 CE 200 200 200 -40 200 200 -40 -40 200 200 -40 200 200 200 -40 200 200 200 -40 200 200 200 200 200 -40 CE 20045 200 X- p> #include< iostream>

使用namespace std;

class Circle {
private:
double * data X-454545454545 X-45454545 X- 20045 X-45454545新新新新新新新新新新旗新新新新旗新新新新旗新新新新旗新新旗新新旗新新旗新旗新旗新旗新新新旗新新新新新旗新新旗新新旗新新新新新旗新新款: b Circle& operator =(const Circle& tt1);
};



Circle :: Circle()
{
cout <默认构造函数<< endl;
data = NUL升;
}

Circle :: Circle(double * set)
{
cout<<< 设置名为<< ENDL;
data = new double [3];
copy(set,set + 3,data);
}

Circle :: Circle(const Circle& tt1)
{
cout<< Copy constructor called<< ENDL;
data = new double [3];
copy(tt1.data,tt1.data + 3,this-> data);
}
Circle ::〜Circle()
{
cout<<< 破坏者叫! << ENDL;
delete [] data;
}

Circle& Circle :: operator =(const Circle& tt1)
{
cout<< Overloaded = called<< ENDL;
if(this!=& tt1)
{
delete [] this-> data;
this-> data = new double [3];
copy(tt1.data,tt1.data + 3,this-> data);
}
return * this; X-454545454545 X- 20045 X-454545 X-454545 X-454545 X-454545 X-454545 X-454545 X-454545 X- double arr [] = {3,5,8,2};
Circle asd(arr);
cout<<< 测试结束3功能<< ENDL;
return asd;新新200新200新新旗新新新旗新新旗旗新新旗新新新旗新新旗新新旗新新旗新新旗新新旗新旗新新旗新旗新新新旗新新旗新旗新新旗新新旗新新新新旗新新旗新新旗新旗新新旗新新新新旗新新旗名旗号: ;< ENDL;
double arr [] = {16,2,7};
圆z(arr);
圆y = z;
cout<<< -------------通过值测试<< ENDL;
test2(z);
cout<-------------测试返回值-------<< endl;
圆工作= test3();

cout<<< ----------- Relese被分配的数据< ENDL;
return 0;
}


解决方案

因为返回值优化第三个测试用例的复制构造函数调用被编译器优化。


As far as I know you call the copy constructor in the following cases:
1 When instantiating one object and initializing it with values from another object
2 When passing an object by value.
3 When an object is returned from a function by value.
I decided to put this to the test and I made this small program testing this (with messages each time a constructor is called. It seems to work for the first two cases, but not for the third one. I want to find out my mistake. Ideas are welcomed.

#include <iostream>

using namespace std;

class Circle{
private:
    double* data;
public:
    Circle();
    Circle(double* set);
    Circle(const Circle& tt1);
    ~Circle();
    Circle& operator=(const Circle& tt1);
};



Circle :: Circle()
{
    cout << "Default constructor called" << endl;
    data = NULL;
}

Circle :: Circle(double* set)
{
    cout << "Set up constructor called" << endl;
    data = new double[3];
    copy(set, set+3, data);
}

Circle :: Circle(const Circle& tt1)
{
    cout << "Copy constructor called" << endl;
    data = new double[3];
    copy(tt1.data, tt1.data+3, this->data);
}
Circle :: ~Circle()
{
    cout << "Destructor called!" << endl;
    delete[] data;
}

Circle& Circle :: operator=(const Circle& tt1)
{
    cout << "Overloaded = called" << endl;
    if(this != &tt1)
    {
        delete[] this->data;
        this->data  = new double[3];
        copy(tt1.data, tt1.data+3, this->data);
    }
    return *this;
}

void test2(Circle a)
{

}
Circle test3()
{
    double arr [] = { 3, 5, 8, 2};
    Circle asd(arr);
    cout<< "end of test 3 function" << endl;
    return asd;
}

int main()
{
    cout <<"-------------Test for initialization" << endl;
    double arr [] = { 16, 2, 7};
   Circle z(arr);
    Circle y = z;
   cout << "-------------Test for pass by value" << endl;
   test2(z);
   cout <<"------------- Test for return value-------"<<endl;
   Circle work = test3();

    cout<< "-----------Relese allocated data" << endl;
    return 0;
}

解决方案

Because of Return Value Optimization the 3rd test case's copy constructor call is optimized away by the compiler.

这篇关于复制未调用的构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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