深浅拷贝比较探究

来源:互联网 发布:破解版阅读软件 编辑:程序博客网 时间:2024/05/19 22:26

  • 什么是深浅拷贝
  • 默认构造默认拷贝构造
    • 类代码
    • 测试代码
    • 结论
  • 自定义构造默认拷贝构造
    • 类代码
    • 测试代码
    • 结论
  • 自定义构造自定义拷贝构造
    • 类代码
    • 测试代码
    • 结论
  • 默认构造默认拷贝构造智能指针成员
    • 类代码
    • 测试代码
    • 结论


什么是深浅拷贝

深拷贝:完全复制,有指针或引用成员的,重新分配内存,复制相同的内容到新分配的内存里
浅拷贝:表象复制,有指针或引用成员的,不再分配内存,指针和引用指向已分配的内存

默认构造+默认拷贝构造

类代码

// default constructor// default copy constructorclass ExampleA {public:    void show() {        cout << "cout << m_a << endl;" << endl;        cout << m_a << endl;        cout << "cout << mp_a << endl;" << endl;        if(mp_a) {            cout << *mp_a << endl;        } else {            cout << 0 << endl;        }    }    int m_a;    int* mp_a;};

测试代码

void TestA() {    cout << "ExampleA has default constructor and default copy constructor:" << endl;    int x = 1;    ExampleA a;    a.m_a = 1;    a.mp_a = &x;    ExampleA b = a;    a.show();    b.show();    a.m_a = 10;    x = 10;    a.show();    b.show();    cout << "a.mp_a:" << a.mp_a << endl;    cout << "b.mp_a:" << b.mp_a << endl;    cout << "Result: general member variable has nothing to do with, point member variable points to same memory, that is, means shallow copy" << endl;}

结论

一般成员变量会直接拷贝,指针成员变量会指向同一块内存,即就是发生了浅拷贝


自定义构造+默认拷贝构造

类代码

// custom constructor// default copy constructorclass ExampleB {public:    ExampleB(int a, int* p_a) {        m_a = a;        mp_a = p_a;    }    void show() {        cout << "cout << m_a << endl;" << endl;        cout << m_a << endl;        cout << "cout << mp_a << endl;" << endl;        if(mp_a) {            cout << *mp_a << endl;        } else {            cout << 0 << endl;        }    }    int m_a;    int* mp_a;};

测试代码

void TestB() {    cout << "ExampleB has custom constructor and default copy constructor:" << endl;    int x = 1;    ExampleB a(1, &x);    ExampleB b = a;    a.m_a = 1;    a.show();    b.show();    a.m_a = 10;    x = 10;    a.show();    b.show();    cout << "a.mp_a:" << a.mp_a << endl;    cout << "b.mp_a:" << b.mp_a << endl;    cout << "Result: general member variable has nothing to do with, point member variable points to same memory, that is, means shallow copy" << endl;}

结论

一般成员变量会直接拷贝,指针成员变量会指向同一块内存,即就是发生了浅拷贝


自定义构造+自定义拷贝构造

类代码

class ExampleC {public:    ExampleC(int a, int* p_a) {        m_a = a;        mp_a = p_a;    }    ExampleC(const ExampleC& exampleC) {        m_a = exampleC.m_a;        mp_a = new int(*exampleC.mp_a);    }    void show() {        cout << "cout << m_a << endl;" << endl;        cout << m_a << endl;        cout << "cout << mp_a << endl;" << endl;        if(mp_a) {            cout << *mp_a << endl;        } else {            cout << 0 << endl;        }    }    int m_a;    int* mp_a;};

测试代码

void TestC() {    cout << "ExampleC has custom constructor and custom copy constructor:" << endl;    int x = 1;    ExampleC a(1, &x);    ExampleC b = a;    a.m_a = 1;    a.show();    b.show();    a.m_a = 10;    x = 10;    a.show();    b.show();    cout << "a.mp_a:" << a.mp_a << endl;    cout << "b.mp_a:" << b.mp_a << endl;    cout << "Result: both general member variable and point member variable have nothing to do with, that is, means deep copy" << endl;}

结论

一般成员变量和指针成员变量所指内容都已拷贝,发生了深拷贝


默认构造+默认拷贝构造+智能指针成员

类代码

// default constructor// default copy constructor// shared_ptr member variableclass ExampleD {public:    void show() {        cout << "cout << m_a << endl;" << endl;        cout << m_a << endl;        cout << "cout << mp_a << endl;" << endl;        if(mp_a) {            cout << *mp_a << endl;        } else {            cout << 0 << endl;        }    }    int m_a;    shared_ptr<int> mp_a;};

测试代码

void TestD() {    cout << "ExampleD has default constructor and default copy constructor:" << endl;    cout << "ExampleD has shared_ptr member variable:" << endl;    ExampleD a;    a.m_a = 1;    a.mp_a = make_shared<int>(1);    ExampleD b = a;    a.show();    b.show();    a.m_a = 10;    a.mp_a = make_shared<int>(10);    a.show();    b.show();    cout << "a.mp_a.use_count():" << a.mp_a.use_count() << endl;    cout << "b.mp_a.use_count():" << b.mp_a.use_count() << endl;    cout << "a.mp_a.get():" << a.mp_a.get() << endl;    cout << "b.mp_a.get():" << b.mp_a.get() << endl;    cout << "Result: both general member variable and point member variable have nothing to do with, that is, means shared_ptr could deal with problems in shallow copy" << endl;}

结论

智能指针可以解决指针浅拷贝问题


CSDN 辣鸡 MD 编辑器,无序列表格式全丢

原创粉丝点击