C語言中用回呼函式模仿C#中的事件
1 #include <stdio.h> 2 void (*func) (void); //定義一個函式指標func 3 4 //呼叫該函式相當于觸發了事件, 5 //該事件觸發后,會檢查函式指標func是否為NULL,如果不為NULL,說明該指標已被賦值(相當于該事件被注冊), 6 //如果事件已被注冊,則執行之, 7 extern void fireTheEvent() 8 { 9 if(func != NULL) 10 { 11 func(); 12 } 13 } 14 15 extern void registerTheEvent(void (*function) (void)) //為fireTheEvent事件注冊監聽器, 16 { 17 func = function; 18 } 19 20 extern void callBack(void) 21 { 22 printf("Hello, this is an event\n"); 23 }
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/172094.html
標籤:C
