#include #include #include "obj.h" #include "cookie.h" using namespace std; void *operator new(size_t n); void operator delete(void *p); int main(int argc, char **argv) { const obj *const pd = new obj(10); //call operator new in line 38 delete pd; //call operator delete in line 49 cout << "\n"; cookie *const pc1 = new cookie(10); //call cookie::operator new delete pc1; //call cookie::operator delete cout << "\n"; cookie *const pc2 = new cookie[3]; //call standard lib operator new[] delete[] pc2; //call standard lib operator delete[] cout << "\n"; cookie *const pc3 = ::new cookie(30); //call operator new in line 38 ::delete pc3; //call operator delete in line 49 cout << "\n"; cookie *const pc4 = ::new cookie(40); //call operator new in line 38 delete pc4; //deliberate mismatch: call cookie::operator delete return EXIT_SUCCESS; } void *operator new(size_t n) { if (void *const p = malloc(n)) { cout << "operator new(" << n << ") returns " << p << ".\n"; return p; } cerr << "operator new(" << n << ") out of store.\n"; exit (EXIT_FAILURE); } void operator delete(void *p) { cout << "operator delete(" << p << ")\n"; free(p); }