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

  1. //: C21:FuncObject.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. // Simple function objects
  7. #include <iostream>
  8. using namespace std;
  9.  
  10. template<class UnaryFunc, class T>
  11. void callFunc(T& x, UnaryFunc f) {
  12.   f(x);
  13. }
  14.  
  15. void g(int& x) {
  16.   x = 47;
  17. }
  18.  
  19. struct UFunc {
  20.   void operator()(int& x) {
  21.     x = 48;
  22.   }
  23. };
  24.  
  25. int main() {
  26.   int y = 0;
  27.   callFunc(y, g);
  28.   cout << y << endl;
  29.   y = 0;
  30.   callFunc(y, UFunc());
  31.   cout << y << endl;
  32. } ///:~
  33.