Here's a generic test, for discussion purposes...
#include #include struct test{
int a;
int b;
int c;
void \* operator new(size\_t);
void operator delete(void\*);
void \* operator new\[\] (size\_t);
void operator delete\[\] (void\*);
};
void * test::operator new(size_t sz)
{
return malloc(sz);
}
void __cdecl test::operator delete(void* t)
{
free(t);
}
void * test::operator new[](size_t sz)
{
return malloc(sz);
}
void test::operator delete[](void* t)
{
free(t);
}
int main(int argc, char* argv[])
{
printf("Hello World!\n");
// calls t::new & t::delete
test\* t = new test;
delete t;
// calls t::new\[\] & t::delete\[\]
t = new test\[20\];
delete \[\]t;
// calls global new and delete
int \* p = new int;
delete p;
return 0;
}
This works as 'expected' on both Borland CPP Builder 4 and MS VC6, but leaves me with another question - I didn't expect to be able to provide an operator delete[] at class scope! Interestingly, if I try to scope the new and delete of the test class (e.g. test::new), I get errors - Borland simply gives 'Expression syntax', whereas VC6 says 'operator xxx must be globally qualified'. What gives?