美文网首页
C++堆与拷贝构造函数

C++堆与拷贝构造函数

作者: 帅碧 | 来源:发表于2016-11-14 20:47 被阅读0次

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

相关文章

  • C++ 拷贝控制(二) — 移动构造函数和移动赋值运算符

    相关文章: C++ 拷贝控制(一) — 析构函数、拷贝构造函数与拷贝赋值函数 C++ 引用类型 — 左值引用、常引...

  • C++堆与拷贝构造函数

    c++创建malloc 简单版 使用堆空间的原因 直到运行时才能知道需要多少对象空间;不知道对象的生存期到底有多长...

  • C++ 构造函数,类的成员变量

    c++ 05 构造函数无参构造函数有参构造函数 拷贝构造函数 浅拷贝 深拷贝 类的成员变量 四类特殊的成员变量

  • [C++之旅] 12 拷贝构造函数

    [C++之旅] 12 拷贝构造函数 拷贝构造函数的特点 如果没有自定义的拷贝构造函数则系统自动生成一个默认的拷贝构...

  • c++学习笔记2(GeekBand)

    拷贝构造、拷贝赋值和析构 c++中有Big Three三个特殊的函数,他们就是拷贝构造函数,拷贝赋值函数和析构函数...

  • C++ 拷贝构造函数浅析

    什么是拷贝构造函数:拷贝构造函数,顾名思义,就是在拷贝的时候调用的构造函数。 几个原则:C++ primer p4...

  • C++拷贝构造函数——难点

    拷贝构造函数 - C++详细 | 编程字典

  • C++基础-(堆与拷贝构造函数)

    堆 link C++对malloc的升级 使用堆空间的原因直到运行时才知道需要多少空间不知道对象的生存期到底有多长...

  • C++:面向对象基础

    构造函数 C++中有三种构造函数:默认构造函数,有参构造函数,拷贝构造函数 类对象的初始化 括号法//默认构造函数...

  • c++第二周笔记

    C++ 第二周笔记 本周的内容比较多,主要介绍了三个重要函数: 拷贝构造、拷贝赋值、析构函数。 1.拷贝构造函数。...

网友评论

      本文标题:C++堆与拷贝构造函数

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