我爱学习网-上传
当前位置: 主页 > 编程 > C++ >

C++ 静态成员变量 嵌套类分配回收内存

时间:2013-04-29 21:41来源:我爱学习网 作者:布丁点儿 点击:

  嵌套类分配回收内存:

  #include <stdio.h>

  #include <assert.h>

  class CA

  {

  public:

  inline static CA *GetInstance(void)

  {

  assert(m_instance != NULL);

  return m_instance;

  }

  void Print(void)

  {

  puts("主类的Print函数运行看看");

  }

  class Garbage//用来分配内存的嵌套类

  {

  public:

  Garbage(int i, int j)

  {

  printf("Garbage分配内存开始:%d %d\n", i, j);

  m_instance = new CA(123123, 234234234);

  }

  ~Garbage()

  {

  delete m_instance;

  puts("Garbage收拾残局结束");

  }

  };

  static Garbage m_garbage;

  protected:

  friend class Garbage;

  CA(int i, int j){printf("主类构造函数运行: %d %d\n", i, j);}

  ~CA(){printf("主类析构函数运行,结束了哦\n");}

  static CA *m_instance;

  };

  CA *CA::m_instance = NULL;

  CA::Garbage CA::m_garbage(2, 3);

  int main()

  {

  CA *cmb = CA::GetInstance();

  cmb->Print();

  return 0;

  }

------分隔线----------------------------

栏目列表
看看啦