成員函數(shù)的指針:通過對象及成員函數(shù)指針來調(diào)用類的成員函數(shù),僅限于非Static成員函數(shù),Static成員函數(shù)通過類名加域限制符直接調(diào)用,無需成員指針;
下面是成員函數(shù)指針的用法及函數(shù)表的使用示例:
1
#include <iostream>
2
3
using namespace std;
4
5
class Screen
6
{
7
8
private:
9
typedef void (Screen::* Action)();
10
static Action menu[];
11
public:
12
void home()
13
{
14
cout<<"home"<<endl;
15
}
16
void left()
17
{
18
cout<<"left"<<endl;
19
}
20
void right()
21
{
22
cout<<"right"<<endl;
23
}
24
void down()
25
{
26
cout<<"down"<<endl;
27
}
28
void up()
29
{
30
cout<<"up"<<endl;
31
}
32
33
public:
34
enum Directions{HOME,LEFT,RIGHT,DOWN,UP};
35
void move(Directions);
36
};
37
38
39
void Screen::move(Directions d)
40
{
41
(this->*menu[d])();
42
}
43
44
Screen::Action Screen::menu[]={
45
&Screen::home,
46
&Screen::left,
47
&Screen::right,
48
&Screen::down,
49
&Screen::up
50
};
51
52
53
int main()
54
{
55
Screen s;
56
57
s.move(Screen::LEFT);
58
59
return 0;
60
}

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60
