美文网首页
C++的深拷贝与浅拷贝

C++的深拷贝与浅拷贝

作者: 东风谷123Liter | 来源:发表于2017-11-25 21:00 被阅读0次

    拷贝构造函数

    拷贝构造函数是使用类对象的引用作为参数的构造函数,它能够将参数的属性值拷贝给新的对象,完成新对象的初始化。
    通常在一下三种情况下,程序会自动调用拷贝构造函数。

    1.使用一个对象初始化另一个对象

    #include <iostream>
    #include<string>
    using namespace std;
    class Car
    {
        public:
            Car(){};    //初始化构造函数
            Car(string name, int seats);
            Car(Car &con_refcar);
            ~Car();    //析构函数
        private:
            string m_name;
            int m_seats;
    };
    Car::Car(string name, int seats)
    {
        m_name = name;
        m_seats = seats;
    }
    Car::Car(Car &con_refcar)
    {
        m_name = con_refcar.m_name;          
        m_seats = con_refcar.m_seats;
    }
    Car::~Car() {}
    int main()
    {
        Car mynewcar("rrrr2", 4);
        Car myseccar(mynewcar);   //用一个对象对另一个对象初始化
        return 0;
    }
    

    2.对象作为实参传递给函数参数

    #include <iostream>
    #include<string>
    using namespace std;
    class Car
    {
        public:
            Car(){};    //初始化构造函数
            Car(string name, int seats);
            Car(Car &con_refcar);
            void print();
            ~Car();    //析构函数
        private:
            string m_name;
            int m_seats;
    };
    Car::Car(string name, int seats)
    {
        m_name = name;
        m_seats = seats;
    }
    Car::Car(Car &con_refcar)
    {
        m_name = con_refcar.m_name;          
        m_seats = con_refcar.m_seats;
    }
    Car::~Car() {}
    void Car::print()
    {
        cout<<"name: "<<m_name<<endl;
        cout<<"sizes: "<<m_seats<<endl;
    }
    void print_carinfo(Car carinfo)  //对象最为实参传递给函数
    {
        carinfo.print();
    }
    int main()
    {
        Car mynewcar("rrrr2", 4);
        print_carinfo(mynewcar);  //调用该函数
        return 0;
    }
    

    3.函数返回值为类对象,创建临时对象作为返回值

    #include <iostream>
    #include<string>
    using namespace std;
    class Car
    {
        public:
            Car(){};    //初始化构造函数
            Car(string name, int seats);
            Car(Car &con_refcar);
            Car get_carinfo();     //****
            void print();
            ~Car();    //析构函数
        private:
            string m_name;
            int m_seats;
    };
    Car::Car(string name, int seats)
    {
        
        m_name = name;
        m_seats = seats;
    }
    Car::Car(Car &con_refcar)
    {
    
        m_name = con_refcar.m_name;          
        m_seats = con_refcar.m_seats;
    }
    Car::~Car()
    {
    }
    void Car::print()
    {
        cout<<"name: "<<m_name<<endl;
        cout<<"sizes: "<<m_seats<<endl;
    }
    Car Car::get_carinfo()    //关键函数
    {
        Car tmp(m_name, m_seats);      //定义类对象
        return tmp;       //返回类对象
    } 
    int main()
    {
        Car mynewcar("rrrr2", 4);
        Car ret;
        ret = mynewcar.get_carinfo();     //ret接受返回的类对象
        ret.print();
        return 0;
    }
    

    以上都是浅拷贝, 浅拷贝有个特点就是:类的私有成员不需要new出新的空间,像上面的m_name都是用string类型;如果换成char类型,上面的程序就会出错,原因是,浅拷贝并未为拷贝对象开辟空间,只是让拷贝对象的地址指向被拷贝对象的地址,相当于“引用”。为了解决这个问题,就要用到深拷贝!

    深拷贝

    #include <iostream>
    #include<string>
    using namespace std;
    class Car
    {
        public:
            Car(char *name, int seats);     
            Car(Car &con_refcar);
            ~Car();
        private:
            char *m_name;     //char型数据成员
            int m_seats;
    };
    Car::Car(char *name, int seats)
    {
        int len = strlen(name) + 1;
        m_name = new char[len];      //每个对象初始化都要临时开劈空间
        strcpy(m_name, name );      //字符串之间赋值要用strcpy()函数
        m_seats = seats;
    }
    Car::Car(Car &con_refcar)       //深拷贝
    {
    
        m_name = new char[strlen(con_refcar.m_name)+1];    //浅拷贝无需开劈空间,数据成员赋值需要开辟空间的都只能用深拷贝实现!
        strcpy(m_name,con_refcar.m_name);       
        m_seats = con_refcar.m_seats;
    }
    Car::~Car()
    {
        delete[] m_name;
    }
    int main()
    {
        char c[] = "rrrr2";
        Car mynewcar(c, 4);
        Car myseccar(mynewcar);
        return 0;
    }
    

    若有错,望指出

    相关文章

      网友评论

          本文标题:C++的深拷贝与浅拷贝

          本文链接:https://www.haomeiwen.com/subject/xhzcbxtx.html