1. 程式人生 > >c++拷貝構造函數(深拷貝、淺拷貝)——轉

c++拷貝構造函數(深拷貝、淺拷貝)——轉

完全復制 pla isp std c++ 必須 spa 例如 成員

拷貝構造函數:

拷貝構造函數是一種特殊的構造函數,函數的名稱必須和類名稱一致,它的唯一的一個參數是本類的一個引用變量,該參數是const類型,不可變的。例如:類A的拷貝構造函數的形式為A(A& x)。當用一個已經初始化過了的自定義類類型對象去初始化另一個新構造的對象的時候,拷貝函數就會被自動調用。也就是說,當類的對象需要拷貝時,拷貝構造函數將會被調用。以下情況會自動調用拷貝構造函數:

  1、一個對象以值傳遞的方式傳入函數體

  2、一個對象以值傳遞的方式從函數返回,如下:

技術分享圖片
#include <iostream>
using namespace std;
class B
{
public: B(){cout<<"構造函數B"<<endl;} B(int i):data(i){cout<<"構造函數B"<<" "<<data<<endl;} B play(B b){return b;} ~B(){cout<<"析構函數B"<<endl;} private: int data; }; int main() { B temp; temp.play(5); return 0; }
View Code

  3、一個對象需要通過另一個對象進行初始化

如果在類中沒有顯示地聲明一個拷貝構造函數,那麽編譯器會自動生成一個默認的拷貝構造函數,該構造函數完成對象之間的拷貝。自定義拷貝構造函數是一種良好的編程風格,它可以阻止編譯器形成默認的拷貝構造函數,提高源碼效率。

深拷貝和淺拷貝:

在某些情況下,類內成員變量需要動態開辟堆內存,如果實行位拷貝(位拷貝又稱淺拷貝),也就是把對象裏面的值完全復制給另一個對象,如A=B。這時,如果B中有一個成員變量指針已經申請了內存,那A中的那個成員也指向同一塊內存。這就出現了問題:把B內存釋放了,這時A內的指針就是野指針了,出現運行錯誤。

深拷貝和淺拷貝可以簡單理解為:如果一個類擁有資源,當這個類的對象發生復制過程的時候,資源重新分配,這個過程就是深拷貝,反之沒有重新分配資源就是淺拷貝。

淺拷貝:

#include <iostream>
#include <string.h>
using namespace std;
class Example
{
private:
    int a;
    char *str;
public:
    Example()
    {
        cout<<"調用構造函數"<<endl;
    }
    ~Example()
    {
        cout<<"調用析構函數"<<endl;
    }

    Example(int b)
    {
        a=b;
cout<<"調用構造函數"<<endl;
    }
    Example(const Example& example)//自定義拷貝構造函數
    {
        a=example.a;
    }
    void show()
    {
        cout<<a<<endl;
    }
};

int main()
{
    Example a1(10);
    Example a2=a1;
    a2.show();
    return 0;
}

輸出:

  調用構造函數

  10

  調用析構函數

  調用析構函數

深拷貝:

#include <iostream>
#include <string.h>
using namespace std;
class Example
{
private:
    int a;
    char *str;
public:
    Example()
    {
        cout<<"調用構造函數"<<endl;
    }
    ~Example()
    {
        delete str;
        cout<<"調用析構函數"<<endl;
    }

    Example(int b,char* e_str)
    {
        a=b;
        str=new char[b];
        strcpy(str,e_str);
     cout<<"調用構造函數"<<endl;
    }
    Example(const Example& example)//自定義拷貝構造函數
    {
        a=example.a;
        str=new char[a];//深拷貝
        if(str!=0)
            strcpy(str,example.str);
    }
    void show()
    {
        cout<<str<<endl;
    }
};

int main()
{
    Example a1(10,"Hello");
    Example a2=a1;
    a2.show();
    return 0;
}

輸出:

  調用構造函數

  Hello

  調用析構函數

  調用析構函數

參考地址:https://www.cnblogs.com/BlueTzar/articles/1223313.html

c++拷貝構造函數(深拷貝、淺拷貝)——轉