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

  1. //: C11:Pmem2.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. // Pointers to members
  7. #include <iostream>
  8. using namespace std;
  9.  
  10. class Widget {
  11.   void f(int) const {cout << "Widget::f()\n";}
  12.   void g(int) const {cout << "Widget::g()\n";}
  13.   void h(int) const {cout << "Widget::h()\n";}
  14.   void i(int) const {cout << "Widget::i()\n";}
  15.   static const int _count = 4;
  16.   void (Widget::*fptr[_count])(int) const;
  17. public:
  18.   Widget() {
  19.     fptr[0] = &Widget::f; // Full spec required
  20.     fptr[1] = &Widget::g;
  21.     fptr[2] = &Widget::h;
  22.     fptr[3] = &Widget::i;
  23.   }
  24.   void select(int i, int j) {
  25.     if(i < 0 || i >= _count) return;
  26.     (this->*fptr[i])(j);
  27.   }
  28.   int count() { return _count; }
  29. };
  30.  
  31. int main() {
  32.   Widget w;
  33.   for(int i = 0; i < w.count(); i++)
  34.     w.select(i, 47);
  35. } ///:~
  36.