home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C10 / StaticDestructors.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  804 b   |  39 lines

  1. //: C10:StaticDestructors.cpp
  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. // Static object destructors
  7. #include <fstream>
  8. using namespace std;
  9. ofstream out("statdest.out"); // Trace file
  10.  
  11. class Obj {
  12.   char c; // Identifier
  13. public:
  14.   Obj(char cc) : c(cc) {
  15.     out << "Obj::Obj() for " << c << endl;
  16.   }
  17.   ~Obj() {
  18.     out << "Obj::~Obj() for " << c << endl;
  19.   }
  20. };
  21.  
  22. Obj a('a'); // Global (static storage)
  23. // Constructor & destructor always called
  24.  
  25. void f() {
  26.   static Obj b('b');
  27. }
  28.  
  29. void g() {
  30.   static Obj c('c');
  31. }
  32.  
  33. int main() {
  34.   out << "inside main()" << endl;
  35.   f(); // Calls static constructor for b
  36.   // g() not called
  37.   out << "leaving main()" << endl;
  38. } ///:~
  39.