#include #include #include using namespace std; const char *progname; void my_new_handler(); class base { int i; public: base(int initial_i = 0): i(initial_i) { cout << "construct base " << i << "\n"; } virtual ~base() {cout << "destruct base " << i << "\n";} void *operator new(size_t n) { void *const p = ::operator new(n); cout << "base::operator new(" << n << ") returns " << p << "\n"; return p; } void operator delete(void *p, size_t n) { cout << "base::operator delete(" << p << ", " << n << ")\n"; ::operator delete(p); } }; class derived: public base { int j; public: derived(int initial_i = 0, int initial_j = 0) : base(initial_i), j(initial_j) { cout << "construct derived " << initial_i << " " << j << "\n"; } ~derived() {cout << "destruct derived " << j << "\n";} }; int main(int argc, char **argv) { progname = argv[0]; set_new_handler(my_new_handler); base *p = new base(10); delete p; cout << "\n"; p = new derived(10, 20); delete p; return EXIT_SUCCESS; } void my_new_handler() { cerr << progname << ": out of store\n"; exit(EXIT_FAILURE); }