Posted by: rmn on: 01/12/2009
If you ever try to define a public custom operator new while keeping the corresponding delete operator private, you’ll end up unable to compile any code that actually invokes the public operator new. The reasoning is quite interesting.
Consider the following code snippet:
#include <cstdlib>
struct Try {
Try () { /* \o/ */ }
void *operator new (size_t size) {
return malloc(size);
}
private:
void operator delete (void *obj) {
free(obj);
}
};
int main () {
Try *t = new Try();
return 0;
}
antrikot:~/work/sandbox> g++ try.cc try.cc: In function ‘int main()’: try.cc:11: error: ‘static void Try::operator delete(void*)’ is private try.cc:17: error: within this context try.cc:11: error: ‘static void Try::operator delete(void*)’ is private try.cc:17: error: within this context
01/12/2009 at 21:04
Looks like googling this up will get you to the same question on stackoverflow, along with its answer..