home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C21 / Counted.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  719 b   |  34 lines

  1. //: C21:Counted.h
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. // An object that keeps track of itself
  7. #ifndef COUNTED_H
  8. #define COUNTED_H
  9. #include <vector>
  10. #include <iostream>
  11.  
  12. class Counted {
  13.   static int count;
  14.   char* ident;
  15. public:
  16.   Counted(char* id) : ident(id) { count++; }
  17.   ~Counted() { 
  18.     std::cout << ident << " count = " 
  19.       << --count << std::endl;
  20.   }
  21. };
  22.  
  23. int Counted::count = 0;
  24.  
  25. class CountedVector : 
  26.   public std::vector<Counted*> {
  27. public:
  28.   CountedVector(char* id) {
  29.     for(int i = 0; i < 5; i++)
  30.       push_back(new Counted(id));
  31.   }
  32. };
  33. #endif // COUNTED_H ///:~
  34.