栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++深浅拷贝

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++深浅拷贝

浅拷贝:就是简单的赋值拷贝操作,对于指针类型是指向同一块底层空间的
深拷贝:在堆区重新申请的空间,进行的拷贝操作。

浅拷贝实例:

class Person
{
public:
    Person()
    {
        cout << "Person的构造函数" << endl;
    }

    Person(int age, int height)
    {
        _age = age;
        _height = new int(height);//堆上开辟,程序员手动开辟,也需要程序员手动释放
        cout << "Person的有参构造函数" << endl;
    }

    //编译器提供的浅拷贝如下
    Person(const Person &p)
    {
        _age = p._age;
        _height = p._height;//浅拷贝的操作
    }

    ~Person()
    {
        //将堆区开辟的程序释放
        if (_height != NULL)
        {
            delete _height;
            _height = NULL;
        }
        cout << "Person的析构函数" << endl;
    }
    
    int _age;
    int *_height;
};

void test1()
{
    Person p1(12, 160);
    cout << p1._age <<  *p1._height << endl;

    Person p2(p1);
    cout << p2._age <<  *p2._height << endl;
}

int main()
{
    test1();
    return 0;
}

 解析:

        在上述代码中,如果在堆区动态开辟一块内存,在进行拷贝构造的时候,只是简单的值传递,拷贝后指针的指向也指向堆区同一块的内存空间,在调用析构函数的时候,原函数会调用析构函数释放堆区开辟的空间,而拷贝构造函数的指向也是这块空间,也会调用析构函数,进行二次释放,导致内存泄漏的问题。

解决方案:

        深拷贝,在拷贝构造函数中在堆区也开辟一块内存空间,自己释放自己的。
 

class Perso
{
public:
    Person()
    {
        cout << "Person的构造函数" << endl;
    }

    Person(int age, int height)
    {
        _age = age;
        _height = new int(height);//堆上开辟,程序员手动开辟,也需要程序员手动释放
        cout << "Person的有参构造函数" << endl;
    }

    Person(const Person &p)
    {
        _age = p._age;
        
        //浅拷贝的操作
        //_height = p._height;

        //深拷贝
        _height = new int(*p._height);
    }

    ~Person()
    {
        //将堆区开辟的程序释放
        if (_height != NULL)
        {
            delete _height;
            _height = NULL;
        }
        cout << "Person的析构函数" << endl;
    }
    
    int _age;
    int *_height;
};

void test1()
{
    Person p1(12, 160);
    cout << p1._age <<  *p1._height << endl;

    Person p2(p1);
    cout << p2._age <<  *p2._height << endl;
}

int main()
{
    test1();
    return 0;
}

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/980231.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号