自定制placement new/delete,又不遮掩全局空间中标准new/delete的方法
代码
1 #include <new>
2 #include <iostream>
3
4 class StandardNewDeleteForms {
5 public:
6 // normal new/delete
7 static void* operator new(std::size_t size) throw(std::bad_alloc) {
8 return ::operator new(size);
9 }
10 static void operator delete(void* pMemory) throw() {
11 ::operator delete(pMemory);
12 }
13 // standard placement new/delete
14 static void* operator new(std::size_t size, void* ptr) throw() {
15 return ::operator new(size, ptr);
16 }
17 static void operator delete(void* pMemory, void* ptr) throw() {
18 return ::operator delete(pMemory, ptr);
19 }
20 // nothrow new/delete
21 static void* operator new(std::size_t size, const std::nothrow_t &nt) throw() {
22 return ::operator new(size, nt);
23 }
24 static void operator delete(void* pMemory, const std::nothrow_t &) throw() {
25 return ::operator delete(pMemory);
26 }
27 };
28
29 class Widget : public StandardNewDeleteForms {
30 public:
31 using StandardNewDeleteForms::operator new;
32 using StandardNewDeleteForms::operator delete;
33
34 static void* operator new(std::size_t size,
35 std::ostream& logStream) throw(std::bad_alloc);
36 static void operator delete(void *pMemory,
37 std::ostream& logStream) throw();
38 };
在gcc4.4.1上编译通过