固定对象在堆或者栈上分配(还没有实验过)

相信一些朋友也碰见过这样的面试题目,本文尝试着给出解答.

1. 分配对象在堆上

  那么,必然要禁止对象在栈上面分配.一个很简单的办法,就是构造函数私有化(提供额外的接口生成对象),那么在栈上面就不可以分配.可是我们还需要这个对象要被析构,那么可以提供一个接口,显式的释放掉这个接口,也就是说delete也得给他禁掉~~~

  考虑另外一个问题,C++有placement new,我自己new一块内存,然后在你这上面构造.问题就变得很恶心,看来我们只有把new,delete都给他禁掉...

  好了,我们现在知道该怎么做:

  • createInstance()接口产生对象
  • dispose()接口销毁对象
  • new/delete操作符全部不可见

来看我们的代码:

?
class  HeapObject{
public :
     static  HeapObject* createInstance()
     {
         return  new  HeapObject;
     }
     void  dispose()
     {
         delete  this ;
     }
protected :
     HeapObject(){}
     ~HeapObject(){}
 
     static  void * operator new  (std:: size_t  size) throw  (std::bad_alloc)
     {
         return  ::operator new (size);
     }
     static  void * operator new  (std:: size_t  size, const  std::nothrow_t& nothrow_constant) throw ();
     static  void * operator new  (std:: size_t  size, void * ptr) throw ();
     static  void * operator new [] (std:: size_t  size) throw  (std::bad_alloc);
     static  void * operator new [] (std:: size_t  size, const  std::nothrow_t& nothrow_constant) throw ();
     static  void * operator new [] (std:: size_t  size, void * ptr) throw ();
 
     static  void  operator delete  ( void * ptr) throw  ()
     {
         ::operator delete (ptr);
     }
     static  void  operator delete  ( void * ptr, const  std::nothrow_t& nothrow_constant) throw ();
     static  void  operator delete  ( void * ptr, void * voidptr2) throw ();
 
     static  void  operator delete [] ( void * ptr) throw  ();
     static  void  operator delete [] ( void * ptr, const  std::nothrow_t& nothrow_constant) throw ();
     static  void  operator delete [] ( void * ptr, void * voidptr2) throw ();
};

2. 分配对象在栈上

  只能分配对象在栈上面,那么new显然是不能用的,否则就是在堆上面分配对象了,理所当然,delete也得禁掉.

?
class  StackObject
{
public :
     StackObject(){}
     ~StackObject(){}
protected :
     static  void * operator new  (std:: size_t  size) throw  (std::bad_alloc);
     static  void * operator new  (std:: size_t  size, const  std::nothrow_t& nothrow_constant) throw ();
     static  void * operator new  (std:: size_t  size, void * ptr) throw ();
     static  void * operator new [] (std:: size_t  size) throw  (std::bad_alloc);
     static  void * operator new [] (std:: size_t  size, const  std::nothrow_t& nothrow_constant) throw ();
     static  void * operator new [] (std:: size_t  size, void * ptr) throw ();
 
     static  void  operator delete  ( void * ptr) throw  ();
     static  void  operator delete  ( void * ptr, const  std::nothrow_t& nothrow_constant) throw ();
     static  void  operator delete  ( void * ptr, void * voidptr2) throw ();
 
     static  void  operator delete [] ( void * ptr) throw  ();
     static  void  operator delete [] ( void * ptr, const  std::nothrow_t& nothrow_constant) throw ();
     static  void  operator delete [] ( void * ptr, void * voidptr2) throw ();
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值