要求对象产生于Heap之中
考虑如下代码:
classHeapClass
{
public:
voidDestory() const {deletethis;}
private:
~HeapClass(){}
};
HeapClass* ptr = newHeapClass;
ptr->Destory();
这样的调用真是很厉害,想生成非Heap对象都不成了。
对于继承和组合的情况不想多说了,比较无趣的说。
判断某个对象是否位于Heap内
考虑如下代码:
newHeapClass(* newHeapClass);
你觉得编译器应该怎么做?
1.调用operator new
2.调用Constructor
3.调用第二个operator new
4.调用第二个Constructor
但是可以让人足够惊讶,编译器对此并不做承诺,所以实际的实现可能是:
1.调用operator new
2.调用第二个operator new
3.调用Constructor
4.调用第二个Constructor
而VC6是这样实现的。
classHeapClass
{
private:
void* operatornew[](size_tsize);
typedefconstvoid * RawAddress;
voidoperatordelete[](void* ptr);
public:
voidoperatordelete(void *ptr)
{
printf("delete
");
::operatordelete(ptr);
m_address.erase(std::remove(m_address.begin(),m_address.end(),ptr),m_address.end());
return;
}
void* operatornew(size_tsize)
{
printf("new
");
void * ptr = ::operatornew(size);
m_address.push_back(ptr);
returnptr;
}
HeapClass()
{
printf("Constructor!
");
}
HeapClass(constHeapClass&)
{
printf("copy Constructor!
");
}
virtualvoidDestory() const {deletethis;}
virtual ~HeapClass() = 0;
boolisOnHeap() const
{
// const void * rawAddress = dynamic_cast<const void *>(this);
constvoid * rawAddress = (constvoid *)(this);
std::deque<RawAddress>::iteratoriter = std::find(m_address.begin(),m_address.end(),rawAddress);
returniter != m_address.end();
}
private:
staticstd::deque<RawAddress> m_address;
};
HeapClass::~HeapClass(){}
std::deque<HeapClass::RawAddress> HeapClass::m_address;
classDHeapClass:publicHeapClass
{};
我在VC6中写了这个Demo测试了一下,但是const void * rawAddress = dynamic_cast<const void *>(this);会出现异常,这让我觉得很郁闷,所以这个Demo只能支持普通的继承方式,不支持多种继承和虚拟继承。
禁止对象产生于heap之中
考虑如下代码:
classHeapClass
{
private:
void* operatornew(size_tsize);
void* operatornew[](size_tsize);
voidoperatordelete(void *ptr);
voidoperatordelete[](void* ptr);
public:
HeapClass(){printf("Constructor!
");}
HeapClass(constHeapClass&){printf("copy Constructor!
");}
public:
~HeapClass(){}
};
这确实是比较简单的事情。