#include #include #include #include "date.h" using namespace std; void my_new_handler(); const char *progname; int main(int argc, char **argv) { progname = argv[0]; set_new_handler(my_new_handler); cout << "How many date's do you want to allocate? "; size_t n; cin >> n; date *const p = reinterpret_cast(new char[n * sizeof (date)]); //Call the constructor for each date in the array. for (date *q = p; q < p + n; ++q) { cout << "Month, day, year for date " << q - p << ": "; int month, day, year; //uninitialized variables cin >> month >> day >> year; new(q) date(month, day, year); //the placement syntax } for (const date *q = p; q < p + n; ++q) { cout << *q << "\n"; } cout << "The hidden numbers are " //unofficial << reinterpret_cast(p)[-2] << " and " << reinterpret_cast(p)[-1] << ".\n"; //Call the destructor for each date in the array. //(Required if class date has a destructor; does nothing otherwise.) for (const date *q = p + n - 1; q >= p; --q) { q->~date(); } delete[] reinterpret_cast(p); return EXIT_SUCCESS; } void my_new_handler() { cerr << progname << ": out of store\n"; exit(EXIT_SUCCESS); }