overloading class specific operator new/delete with arguments
I want to overload operator new on my class to take an additional
argument. I am therefore required to overload operator delete as well
so that if the constructor throws it will call the matching operator
delete.
I do not want to write additional class specific operator new/delete
that take no additional arguments.
My problem arises when I attempt to delete the pointer returned from
new. As I don't have a "default" operator delete on my class I need
to call the operator delete that takes parameters. The only way I can
find to do this is to call class::operator delete(pointer,args...);
However, this does not call the destructor. So, now I need to
explicitly call the destructor and then explicitly call
class::operator delete.
Is there a syntax to call delete which would pass arguments to the
operator delete after calling the destructor? I tried to match the
syntax of the call to new and that didn't work:
new(new_args) type(constructor_args);
delete(delete_args) pointer;
Any suggestions? Any explanation why the above doesn't work (other
than: the standard doesn't allow it). Why doesn't the standard allow
it?
e.g.:
#include <new>
struct foo {
//overload operator new
void* operator new(size_t, const char *) throw();
//corresponding operator delete to match
void operator delete(void *, const char *);
//constructor/destrucot
foo(int,int);
~foo();
};
void bar() {
foo * pfoo = new("hello") foo(1,2);
//no appropriate operator delete visible
// delete pfoo;
//syntax error
// delete("goodbye") pfoo;
//ugly
pfoo->~foo();
foo::operator delete(pfoo,"goodbye");
}
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]