f****4 发帖数: 1359 | 1 class Mem{
public:
void* operator new(size_t n, int a);
void operator delete(void* p);
};
Mem* a = new(10) Mem;
a->~Mem();
delete a;
// operator new & delete work correctly
void* buf = malloc(sizeof(Mem)+sizeof(int));
Mem* b = new(buf) Mem; // wrong
b->~Mem();
free(buf);
我发现只要重载了operaor new之后,缺省的placement new的表达就出错了
重载 void* operator new(size_t n, void* buf);之后又好了
问题是我重载的这个void* operator new(size_t n, void* buf),是不是placement
new?有人说placement new是不能重载的(http://cyclopedia.name/post/e6b58 | h****8 发帖数: 599 | 2 是不是因为 Mem* b = new(buf) Mem;调用的是class Mem中的被重载的new
如果换成 Mem* b = ::new(buf) Mem;试试看呢 | x***y 发帖数: 633 | 3 When you write custom versions of operator new, all the 3 form of operator
new in global space won't be visible until you explicitly write them via
forwarding function ::operator new(...)..
【在 f****4 的大作中提到】 : class Mem{ : public: : void* operator new(size_t n, int a); : void operator delete(void* p); : }; : Mem* a = new(10) Mem; : a->~Mem(); : delete a; : // operator new & delete work correctly : void* buf = malloc(sizeof(Mem)+sizeof(int));
|
|