我想定义我自己的放置新的和放置删除(带额外的参数),我发现我可以正确调用放置,而我无法访问放置删除。任何人都可以告诉我是否我错误地定义了位置删除或者我错误地调用了它?C++自定义的放置新的和放置删除调用
class A
{
public:
A(int a) : a(a){}
static void* operator new(std::size_t, int); // the placement new
static void operator delete(void*, int)throw(); // the corresponding placement delete
private:
int a;
};
void* A::operator new(std::size_t size, int n)
{
std::cout << "size: " << size << " " << "n: " << n << std::endl;
return ::operator new(size);
}
void A::operator delete(void* p, int n)throw()
{
std::cout << "n: " << n << std::endl;
::operator delete(p);
}
int main(int argc, char* argv[])
{
A* a = new(10) A(100);
std::cout << std::endl;
delete(4) a; // error???????????????????, but how?
return 0;
}
[为什么在C++中没有放置删除表达式?](http://stackoverflow.com/questions/5857240/why-there-is-no-placement-delete-expression-in-c) – Suma
这看起来不像新的位置,特别是你实例化你的'A *'指针的方式(不确定10是否是正确的内存位置:P)。看看这里:http://www.parashift.com/c++-faq/placement-new.html – Xaqq
@Xaqq:它是一种新的布局形式,但不是最常见的一种,它通过地址该对象将被放置。 –