要求对象产生于heap中,意思是需要阻止clients不得使用new以外的方法产生对象。比较好的方法就是将destructor定义为private,因为constructor的类型太多,所以仍然将constructor定义为public。然后定义一个pseudo destructor来调用真正的destructor。示例如下:
class HeapBasedObject { public: HeapBasedObject() {} void destroy() const { delete this; } // pseudo destructor private: ~HeapBasedObject() {} }; int main() { //HeapBasedObject h; HeapBasedObject *ph = new HeapBasedObject; //delete ph; ph->destroy(); }
禁止对象产生于heap中,则是要让clients不能使用new方法来产生对象。方法就是将operator new和operator delete定义为private。示例如下:
#include <stdlib.h> class NoHeapBasedObject { public: NoHeapBasedObject() {} ~NoHeapBasedObject() {} private: static void *operator new(size_t size) {} static void operator delete(void *ptr) {} }; int main() { NoHeapBasedObject nh; static NoHeapBasedObject snh; //NoHeapBasedObject *pnh = new NoHeapBasedObject; }
下例是实现的一个判断某个对象是否位于heap内的基类HeapTracked。
#include <stdlib.h> #include <list> #include <iostream> class HeapTracked { public: class MissingAddress{}; //地址异常 virtual ~HeapTracked() = 0; static void *operator new(size_t size); static void operator delete(void *ptr); bool isOnHeap() const; private: typedef const void* RawAddress; static std::list<RawAddress> addresses; }; std::list<HeapTracked::RawAddress> HeapTracked::addresses; HeapTracked::~HeapTracked() {} void* HeapTracked::operator new(size_t size) { void* memPtr = ::operator new(size); // 取得内存。 addresses.push_front(memPtr); // 将其地址置于list头部。 return memPtr; } void HeapTracked::operator delete(void *ptr) { auto it = std::find(addresses.begin(), addresses.end(), ptr); if (it != addresses.end()) { addresses.erase(it); ::operator delete(ptr); } else { throw MissingAddress(); } } bool HeapTracked::isOnHeap() const { auto rawAddress = dynamic_cast<const void*>(this); auto it = std::find(addresses.begin(), addresses.end(), rawAddress); return it != addresses.end(); } class Object: public HeapTracked { public: Object() {} ~Object() {} }; int main() { Object o1; Object* o2 = new Object; std::cout << "o1 isOnHeap = " << o1.isOnHeap() << std::endl; std::cout << "o2 isOnHeap = " << o2->isOnHeap() << std::endl; }
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。