More Effective C++ 27. Requiring or Prohibiting Heap-Based objects
来源:互联网 发布:sweet alert.js 编辑:程序博客网 时间:2024/06/05 07:26
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 list<RawAddress> addresses;};// to make the class abstract. the destructor must still be defined, so // we provide this empty definition.HeapTracked::~HeapTracked() { };void* HeapTracked::operator new(size_t size) { void* memPtr = ::operator new(size); addresses.push_back(memPtr); return memPtr;}void HeapTracked::operator delete(void *ptr) { // gracefully handle null pointers if (ptr == 0) return; list<RawAddress>::iterator it = find(addresses.begin(), addresses.end(), ptr); if (it != addresses.end()) { addresses.erase(it); ::operator delete(ptr); } else { throw MissingAddress(); }}bool HeapTracked::isOnHeap() const { const void* rawAddress = dynamic_cast<const void*>(this); list<RawAddress>::iterator it = find(addresses.begin(), addresses.end(), rawAddress); return it != addresses.end();}
it can not be used with the built-in types. because types like int and char can not inherit from anything. And built-in type have no this pointer.
阅读全文
0 0
- More Effective C++ 27. Requiring or Prohibiting Heap-Based objects
- Creating objects on stack or heap
- 《more effective c++》读书笔记
- More Effective C++(2)
- 《More Effective C++》读后感
- 《More Effective C++》笔记
- 《More Effective C++》学习心得(八) 要求(或禁止)对象产生于heap中
- 《Effective C++》和《More Effective C++》汇总
- 《More Effective C++》读书笔记一
- 《more effective c++》笔记4
- More Effective C++:类型转换
- More Effective C++:类型转换
- More Effective C++:Item 27
- more effective c++--引用计数
- 读More Effective C++(1)
- More Effective C++:Item 27
- 《More Effective C++》读书笔记-异常
- 《More Effective C++》读书笔记-效率
- 使用js和layer.js实现漂亮的验证效果
- easyUI的DataGrid(数据表格)日期格式化(自学,不喜勿喷,欢迎指导,谢谢)
- 第四周实践项目3单链表:逆置、连接与递增判断(包含三个程序)
- Koa 框架教程
- 一分钟了解“matlab产生不重复的随机数序列”
- More Effective C++ 27. Requiring or Prohibiting Heap-Based objects
- Curator典型使用场景之分布式锁。
- IP命令
- 欢迎使用CSDN-markdown编辑器
- 基于私有云的数据库高可用架构实践
- lnmp环境搭建
- 【笔记】php数字格式(1)- str_pad
- 解决动态高度的iframe加载后双滚动条的问题
- vue 使用百度编辑器ueditor