20141102

//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;  
  }

 

posted @ 2014-11-03 00:41  yexuannan  阅读(158)  评论(0编辑  收藏  举报