码迷,mamicode.com
首页 > 其他好文 > 详细

20141102

时间:2014-11-03 01:15:42      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   io   color   os   sp   div   on   

//HeapOnly.cpp  只能在堆或者栈上分配内存的类
  #include   <iostream>  
  using   namespace   std;  
   
  class   HeapOnly  
  {  
  public:  
  HeapOnly()   {   cout   <<   "constructor."   <<   endl;   }  
  void   destroy   ()   const   {   delete   this;   }  
  private:  
  ~HeapOnly()   {}    
  };  
   
  int   main()  
  {  
  HeapOnly   *p   =   new   HeapOnly;  
  p->destroy();  
  // HeapOnly   h;  
  // h.Output();  
   
  return   0;  
  }  
  //StackOnly.cpp  
  //2005.07.18------2009.06.05  
  #include   <iostream>  
  using   namespace   std;  
   
  class   StackOnly  
  {  
  public:  
  StackOnly()   {   cout   <<   "constructor."   <<   endl;   }  
  ~StackOnly()   {   cout   <<   "destructor."   <<   endl;   }  
  private:  
  void*   operator   new   (size_t);  
  };  
   
  int   main()  
  {  
  StackOnly   s;                                                             //okay  
  StackOnly   *p   =   new   StackOnly;                           //wrong  
   
  return   0;  
  }

 

20141102

标签:des   style   blog   io   color   os   sp   div   on   

原文地址:http://www.cnblogs.com/yexuannan/p/4070331.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!