class Image { int id; static int next; public: Image() { id = next++; cout << " $$ ctor: "<< id << '\n'; } ~Image() { cout << " dtor: " << id << '\n'; } void draw() { cout << " drawing image " << id << '\n'; } }; int Image::next = 1; void main( void ) { Image images[5]; int i; cout << "Exit[0], Image[1-5]: "; cin >> i; while (i) { images[i-1].draw(); cout << "Exit[0], Image[1-5]: "; cin >> i; } } // $$ ctor: 1 // $$ ctor: 2 // $$ ctor: 3 // $$ ctor: 4 // $$ ctor: 5 // Exit[0], Image[1-5]: 2 // drawing image 2 // Exit[0], Image[1-5]: 4 // drawing image 4 // Exit[0], Image[1-5]: 2 // drawing image 2 // Exit[0], Image[1-5]: 0 // dtor: 5 // dtor: 4 // dtor: 3 // dtor: 2 // dtor: 1 #if 0 class RealImage { int id; public: RealImage( int i ) { id = i; cout << " $$ ctor: "<< id << '\n'; } ~RealImage() { cout << " dtor: " << id << '\n'; } void draw() { cout << " drawing image " << id << '\n'; } }; class Image { RealImage* theRealThing; int id; static int next; public: Image() { id = next++; theRealThing = 0; } ~Image() { delete theRealThing; } void draw() { if ( ! theRealThing) theRealThing = new RealImage( id ); theRealThing->draw(); } }; int Image::next = 1; void main( void ) { Image images[5]; int i; cout << "Exit[0], Image[1-5]: "; cin >> i; while (i) { images[i-1].draw(); cout << "Exit[0], Image[1-5]: "; cin >> i; } } // Exit[0], Image[1-5]: 2 // $$ ctor: 2 // drawing image 2 // Exit[0], Image[1-5]: 4 // $$ ctor: 4 // drawing image 4 // Exit[0], Image[1-5]: 2 // drawing image 2 // Exit[0], Image[1-5]: 4 // drawing image 4 // Exit[0], Image[1-5]: 0 // dtor: 4 // dtor: 2 #endif