相信一些朋友也碰见过这样的面试题目,本文尝试着给出解答.
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
();
};
|