一、动态空间申请
#include <iostream>
#include <string.h>
#include <stdlib.h> //需要用到malloc
using namespace std;
//定义一个盒子类
class Box
{
public:
Box() //定义构造函数
{
cout << "Box的无参构造" << endl;
}
~Box() //定义析构函数
{
cout << "Box的析构函数" << endl;
}
int a; //私有成员数据
};
//在C语言中动态申请空间
void test01()
{
//用malloc申请一段动态内存空间
Box *p= (Box*)malloc(sizeof(Box)); //空间大小为类Box的大小
free(p)