Special Classes and Functions 531
15
Arrays of Pointers to Member Functions ......................................................
As with pointers to functions, pointers to member functions can be stored in an array.
The array can be initialized with the addresses of various member functions, and these
can be invoked by offsets into the array. Listing 15.11 illustrates this technique.
LISTING15.11 Array of Pointers to Member Functions
0: //Listing 15.11 Array of pointers to member functions
1: #include <iostream>
2: using std::cout;
3: using std::endl;
4:
5: class Dog
6: {
7: public:
8: void Speak()const { cout << “Woof!” << endl; }
9: void Move() const { cout << “Walking to heel...” << endl; }
10: void Eat() const { cout << “Gobbling food...” << endl; }
11: void Growl() const { cout << “Grrrrr” << endl; }
12: void Whimper() const { cout << “Whining noises...” << endl; }
13: void RollOver() const { cout << “Rolling over...” << endl; }
14: void PlayDead() const
➥ { cout << “The end of Little Caesar?” << endl; }
15: };
16:
17: typedef void (Dog::*PDF)()const ;
18: int main()
19: {
20: const int MaxFuncs = 7;
21: PDF DogFunctions[MaxFuncs] =
22: {Dog::Speak,
23: Dog::Move,
24: Dog::Eat,
25: Dog::Growl,
26: Dog::Whimper,
27: Dog::RollOver,
28: Dog::PlayDead };
29:
30: Dog* pDog =0;
31: int Method;
32: bool fQuit = false;
33:
34: while (!fQuit)
35: {
36: cout << “(0)Quit (1)Speak (2)Move (3)Eat (4)Growl”;
37: cout << “ (5)Whimper (6)Roll Over (7)Play Dead: “;
38: std::cin >> Method;
39: if (Method <= 0 || Method >= 8)