美文网首页
c++ 第四章 第五章:堆与拷贝构造函数

c++ 第四章 第五章:堆与拷贝构造函数

作者: ie大博 | 来源:发表于2016-11-10 14:25 被阅读0次

第四章

代码啥的没有,主要就是讲述一个思想吧,就是要把问题分析,拆分出类,让主函数调用的方便,老师的意思是说要把函数做成具有面向对象的思想的函数,比较直观易懂。(⊙o⊙)…

第五章:堆与拷贝构造函数

c++里面的“malloc”“free”

public:
    test(int a)
    {
        cout<<"test construct"<<endl;
    }
    ~test()
    {
        cout<<"======"<<endl;
    }
};
int main()
{
    test *t=new test(5);//在堆上建造空间,不用malloc,这个位置给构造函数传参
    cout<<"主函数"<<endl;
    delete t;//释放空间
    return 0;
}

数组在堆上的建造和释放

class test
{
public:
    test()
    {
        cout<<"test construct"<<endl;//这个地方会被执行五次
    }
    ~test()
    {
        cout<<"======"<<endl;//这个地方会被执行五次

    }
};
int main()
{
    test *t=new test[5];//在对上建立一个数组
    cout<<"主函数"<<endl;
    delete[] t;//释放空间,这里面的“【】”不能省略,因为释放的是数组空间
    return 0;
}

构造函数的拷贝

class test
{
    int m_a;
public:
    test(int a)//构造函数只能出现一次
    {
        m_a=a;
        cout<<"test construct"<<m_a<<endl;  }
    ~test()
    {
        cout<<"======"<<m_a<<endl;
    }
    void show()
    {
        cout<<"m_a="<<m_a<<endl;
    }
};
void hello(test temp)
{
    temp.show();
}
int main()
{
    test t1(10);
    t1.show();//调用一次

    hello(t1);//同过hello函数调用一次
        return 0;
}

系统给的拷贝的原理

class test
{
    int m_a;
public:
    test(int a)
    {
        m_a=a;
        cout<<"test construct"<<m_a<<endl;  }
    ~test()
    {
        cout<<"======"<<m_a<<endl;
    }
    void show()
    {
        cout<<"m_a="<<m_a<<endl;
    }
    test(const test& q)///这里传的表示test q,因为
    {
        m_a=q.m_a;
    }
};
void hello(test temp)
{
    temp.show();
}
int main()
{
    test t1(10);
    t1.show();

    hello(t1);
        return 0;
}

当传指针的时候,系统给的构造函数的拷贝会出问题,因为少了指针的初始化,然而delete却没少

using namespace std;
class test
{
    int *m_a;//变量是指针
public:
    test(int a)
    {
        m_a=new int;//分配堆空间
        *m_a=a;
        cout<<"alex is "<<*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();
}

相关文章

  • 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/yiqtpttx.html