#include #include #include "date.h" using namespace std; inline void *operator new(size_t n) { void *const p = malloc(n); cout << "operator new(" << n << ") returns " << p << "\n"; return p; } inline void operator delete(void *p) { cout << "operator delete(" << p << ")\n"; free(p); } inline void *operator new[](size_t n) { void *const p = malloc(n); cout << "operator new[](" << n << ") returns " << p << "\n"; return p; } inline void operator delete[](void *p) { cout << "operator delete[](" << p << ")\n"; free(p); } class myclass { public: void *operator new(size_t n) { void *const p = malloc(n); cout << "myclass::operator new(" << n << ") returns " << p << "\n"; return p; } void operator delete(void *p) { cout << "myclass::operator delete(" << p << ")\n"; free(p); } void *operator new[](size_t n) { void *const p = malloc(n); cout << "myclass::operator new[](" << n << ") returns " << p << "\n"; return p; } void operator delete[](void *p) { cout << "myclass::operator delete[](" << p << ")\n"; free(p); } }; int main(int argc, char **argv) { const date *p1 = new date; //calls operator new in line 6 delete p1; //calls operator delete in line 12 p1 = new date[3]; //calls operator new[] in line 17 delete[] p1; //calls operator delete[] in line 23 cout << "\n"; const myclass *p2 = new myclass; //calls myclass::operator new in line 30 delete p2; //calls myclass::operator delete in line 36 p2 = new myclass[3]; //calls myclass::operator new[] in line 41 delete[] p2; //calls myclass::operator delete[] in line 47 cout << "\n"; p2 = ::new myclass; //calls operator new in line 6 ::delete p2; //calls operator delete in line 12 p2 = ::new myclass[3]; //calls operator new[] in line 17 ::delete[] p2; //calls operator delete[] in line 23 return EXIT_SUCCESS; }