C++堆與拷貝構造函數

c++創建malloc

  • 簡單版
#include<iostream>
using namespace std;
class Test
{
public:
    Test()
    {
        cout<<"Test construct"<<endl;
    }
    ~Test()
    {
        cout<<"~~~~~Test"<<endl;
    }
};
int main()
{
    Test *t=new Test;
    cout<<"========="<<endl;
    delete t;
}

//結果為:Test construct
//===========
//~~~~~~~~~Test

#include<iostream>
#include<stdlib.h>
using namespace std;
class Test
{
public:
    Test(int a)
    {
        cout<<"Test construct"<<a<<endl;
    }
    ~Test()
    {
        cout<<"~~~~~Test"<<endl;
    }
};
int main()
{
    Test *t=new Test(5);
    cout<<"========="<<endl;
    delete t;
}

//結果為:Test construct5
//===========
//~~~~~~~~~Test

#include<iostream>
#include<stdlib.h>
using namespace std;
class Test
{
public:
    Test(int a)
    {
        cout<<"Test construct"<<a<<endl;
    }
    Test()
    {
        cout<<"Test construct"<<endl;
    }
    ~Test()
    {
        cout<<"~~~~~Test"<<endl;
    }
};
int main()
{
    Test *t=new Test[5];//只能調用無參的
    cout<<"========="<<endl;
    delete[] t;
}
//運行結果如下
//Test contruct
//Test contruct
//Test contruct
//Test contruct
//Test contruct
//===========
//~~~~~~~~~Test
//~~~~~~~~~Test
//~~~~~~~~~Test
//~~~~~~~~~Test
//~~~~~~~~~Test

使用堆空間的原因

  1. 直到運行時才能知道需要多少對象空間;
  2. 不知道對象的生存期到底有多長;
  3. 直到運行時才知道一個對象需要多少內存空間。

拷貝構造函數

  1. 系統默認的拷貝構造函數
#include<iostream>
using namespace std;
class Test
{
    int m_a;
public:
    Test(int a)
    {
        m_a=a;
        cout<<"Test construct"<<a<<endl;
    }
    ~Test()
    {
        cout<<"~~~~Test"<<endl;
    }
    void show()
    {
        cout<<"m_a = "<<m_a<<endl;
    }
    Test(const Test &q)
    {
        m_a=q.m_a;
    }
};

int main()
{
    Test t1(10);
    t1.show();
    Test t2(t1);
    t2.show();
}

//結果為:
//Test contruct10;
//m_a=10;
//m_a=10;
//~~~~~~~~Test
//~~~~~~~Test

  1. 系統默認的構造函數
#include<iostream>
using namespace std;
class Test
{
    int m_a;
public:
    Test(int a)
    {
        m_a=a;
        cout<<"Test construct"<<a<<endl;
    }
    ~Test()
    {
        cout<<"~~~~Test"<<endl;
    }
    void show()
    {
        cout<<"m_a = "<<m_a<<endl;
    }
    /*Test(const Test &q)
    {
        m_a=q.m_a;
    }*////默認構造函數只能完成淺拷貝,將形參傳給自身

};
void hello(Test temp)
{
    temp.show();
}

int main()
{
    Test t1(10);
    t1.show();
    Test t2(t1)
    t2.show();
    hello(t1);
}


//結果為:
//Test contruct10;
//m_a=10;
//m_a=10;
//~~~~~~~~Test
//~~~~~~~Test


  • 深拷貝
#include<iostream>
using namespace std;
class Test
{
    int *m_a;//定義指針類型時,系統默認的拷貝構造函數已經過不去了
public:
    Test(int a)
    {
        m_a=new int;
        *m_a=a;
        cout<<"Test construct"<<*m_a<<endl;
    }
    ~Test()
    {
        cout<<"~~~~Test"<<*m_a<<endl;
        delete m_a;
    }
    void show()
    {
        cout<<"m_a = "<<*m_a<<endl;
    }
    Test(const Test &q)//所以要認為的重新定義
    {
        m_a=new int;
        *m_a=*q.m_a;
    }
};
int main()
{
    Test t1(10);
    t1.show();
    Test t2(t1);
    t2.show();
}


//結果為:
//Test contruct10;
//m_a=10;
//m_a=10;
//~~~~~~~~Test10
//~~~~~~~Test10

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容