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

  1. //: C10:Statfun.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 vars inside functions
  7. #include "../require.h"
  8. #include <iostream>
  9. using namespace std;
  10.  
  11. char onechar(const char* string = 0) {
  12.   static const char* s;
  13.   if(string) {
  14.     s = string;
  15.     return *s;
  16.   }
  17.   else
  18.     require(s, "un-initialized s");
  19.   if(*s == '\0')
  20.     return 0;
  21.   return *s++;
  22. }
  23.  
  24. char* a = "abcdefghijklmnopqrstuvwxyz";
  25.  
  26. int main() {
  27.   // Onechar(); // require() fails
  28.   onechar(a); // Initializes s to a
  29.   char c;
  30.   while((c = onechar()) != 0)
  31.     cout << c << endl;
  32. } ///:~
  33.