1 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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
#include <iostream> #include <conio.h> #include <Windows.h> #define KEY_ESC 27 using std::cout; using std::endl; class CAccount { public: struct StateMap { int input; void (CAccount::*f)(); }; static StateMap map[]; enum { STATE_IDLE, STATE_INPUT, STATE_WAIT_ACK, STATE_MAX }; public: int m_iState; CAccount(); void OnIdle(); void OnInput(); void OnWaitAck(); }; CAccount::CAccount() { m_iState = STATE_IDLE; } void CAccount::OnIdle() { cout<< "OnIdle()"<<endl; } void CAccount::OnInput() { cout<<"OnInput()"<<endl; } void CAccount::OnWaitAck() { cout<<"OnWaitAck"<<endl; } /*static*/ CAccount::StateMap CAccount::map[]={ '1',&CAccount::OnIdle, '2',&CAccount::OnInput, '3',&CAccount::OnWaitAck, 0,NULL //sentinel }; void main() { int ch=0; int i; CAccount account; while(ch!=KEY_ESC) { ch =getch(); i=0; while(CAccount::map[i].input!=0) { if(ch == CAccount::map[i].input) { (account.*(CAccount::map[i].f))(); } ++i; } } } |