M
Mark P
I have a class in which I've redefined operator new and operator delete.
For example:
class C {
....
void* operator new(size_t size) {do some stuff; return void*}
void operator delete(void *p) {do some stuff;}
}
Then I tried to use an STL container of C objects, say list<C>. This
produced a long compiler error, the gist of which went like:
no matching function for call to `C:perator new(size_t, void*&)`
candidates are: static void* C:perator new(size_t)
Am I right in thinking that the container list<C> tried to invoke
placement new for class C, using memory secured by its own allocator,
but that my definition of operator new has hidden all other definitions
of new and so no matching definition was found?
My seems-to-work solution was to define placement new for C as well and
have it simply call global placement new:
void* operator new(size_t size, void*& loc)
{return perator new(size,loc));}
Is this the right way to deal with this? I know that the "do some
stuff" clause of my ordinary operator new won't be executed, but this is
OK in my case (that will be done by the allocator which calls placement
new).
Also, if an object is created by placement new, then does it always get
deleted by an explicit destructor call rather than by calling operator
delete?
Thanks for your help,
Mark
For example:
class C {
....
void* operator new(size_t size) {do some stuff; return void*}
void operator delete(void *p) {do some stuff;}
}
Then I tried to use an STL container of C objects, say list<C>. This
produced a long compiler error, the gist of which went like:
no matching function for call to `C:perator new(size_t, void*&)`
candidates are: static void* C:perator new(size_t)
Am I right in thinking that the container list<C> tried to invoke
placement new for class C, using memory secured by its own allocator,
but that my definition of operator new has hidden all other definitions
of new and so no matching definition was found?
My seems-to-work solution was to define placement new for C as well and
have it simply call global placement new:
void* operator new(size_t size, void*& loc)
{return perator new(size,loc));}
Is this the right way to deal with this? I know that the "do some
stuff" clause of my ordinary operator new won't be executed, but this is
OK in my case (that will be done by the allocator which calls placement
new).
Also, if an object is created by placement new, then does it always get
deleted by an explicit destructor call rather than by calling operator
delete?
Thanks for your help,
Mark