您的位置:首页 > 编程语言 > C语言/C++

对象创建问题:heapOnly,stackOnly代码(C++代码)

2010-12-03 12:26 260 查看
class   HeapOnly
{
public:
 HeapOnly()
 {  
  cout<<"constructor. "<<endl;  
 }
 void destroy()
 {  
  delete this;  
 }
private:
 ~HeapOnly(){}  
};
 
int main()
{
 HeapOnly   *p = new HeapOnly;
 p->destroy();
 HeapOnly h;
 h.Output();
 
 return 0;
}
 

#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c++ iostream class delete