Re: why isn't there a placement delete syntax
andrew_nuss@yahoo.com wrote:
Hi,
I have created an interface with placement new that uses a Heap
reference that supplies Alloc/Free functionality. The compiler forces
the definition of a placement delete. But there is no syntax that I
can find to invoke the placement delete!
struct Object {
static void* operator new (Heap&, size_t);
static void operator delete (Heap&, void*);
};
class MyObj : public Object {
...
};
main {
Heap heap;
MyObj* p = new (heap) MyObj(); // works!
delete (heap) p; // illlegal!
}
Can anyone explain how to delete these objects. I.e. why doesn't the
obvious analagous invocation of delete work using placement syntax.
What do I do?
If your question is "how do I achieve what a placement delete would have
done?", then the answer will be: just call the destructor:
p->~MyObj();
But if your question is "why, then, there is not a placement delete that
does exactly that?", then the answer would be long list arguments for
and against the topic.
Maybe you can think that a delete is always associated with memory
reclamation; but then why is there a placement new there that doesn't by
itself do any memory allocation? I don't know. The language ain't
perfect, I guess.
There are workarounds, of course. When you are allocating a single
object on your own heap, bury the placement new in some function so at
least you have a symmetry.
If you are allocating for a number of objects of the same type, you can
write yourself an allocator and use the standard containers.
Andy
Regards,
Ben
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]