Humaneer
2014년 11월 19일 수요일
cpp map과 함수포인터를 이용한 메시지 핸들링 기법.
#include
#include
using namespace std; struct msg_a { int msg_id; int a; int b; msg_a() : msg_id(1), a(2), b(3) {} }; struct msg_b { int msg_id; float a; double b; msg_b() : msg_id(2), a(0.51f), b(33.232) {} }; void HandleMsgA(void* pBuf) { cout << "\nmsg_a handler" << endl; msg_a* pMsg = static_cast
(pBuf); cout << "a = " << pMsg->a << endl; cout << "b = " << pMsg->b << endl; } void HandleMsgB(void* pBuf) { cout << "\nmsg_b handler" << endl; msg_b* pMsg = static_cast
(pBuf); cout << "a = " << pMsg->a << endl; cout << "b = " << pMsg->b << endl; } typedef void (*ptr)(void*); typedef map
MsgHandler; int main() { MsgHandler a; a[1] = &HandleMsgA; a[2] = &HandleMsgB; msg_a msgA; msg_b msgB; a[1](&msgA); a[2](&msgB); a.find(100)->second(&msgB); // no error // a[100](&msgB); // runtime-error //a.find(1)->second(&msgA); //a.find(2)->second(&msgB); // your code goes here return 0; }
댓글 없음:
댓글 쓰기
최근 게시물
이전 게시물
홈
피드 구독하기:
댓글 (Atom)
댓글 없음:
댓글 쓰기