Today I saw an article titled Implementation of window fade-in and fade-out effects, and I saw a callback function problem. I have never seen it before (I am a bird, haha). So I studied it ......
Directly paste the code, which is relatively simple ~
For example, some of the original function parameters are functions, and some pointers are passed in:
Int samplefunction (INT A1, int A2); <br/> int main () <br/>{< br/> typedef int (* callbackfunction) (INT, INT ); </P> <p> callbackfunction bfn = & samplefunction; <br/> int result = bfn (3, 4); <br/> printf ("% d ", result); <br/> return 0; <br/>}</P> <p> int samplefunction (INT A1, int A2) <br/>{< br/> int num = A1 + A2; <br/> return num; <br/>}
Then I checked "typedef" in Baidu encyclopedia and wrote another example:
# Include <stdio. h> <br/> // ======================== Use Pointer ============== = <br/> void fact1 (INT num, int ** KK) <br/>{< br/> * KK = & num; <br/>}< br/> // ============================ ========</P> <p> // ============================== ============< br/> void fact2 (INT num, int & KK) <br/>{< br/> KK = num; <br/>}< br/> // ============================ ========</P> <p> // ===================== use the callback function ====== ========< br/> int fact_back1 (INT num) <br/>{< br/> return num; <br/>}< br/> int fact_back2 (INT (* callback) (INT num )) <br/>{< br/> int cool = 5; <br/> return callback (cool ); <br/>}< br/> // ============================ ========</P> <p> // ================= use the callback function (2) =============< br/> int fact_back1_2 (INT num) <br/>{< br/> return num; <br/>}</P> <p> typedef int (* callback_2) (INT); </P> <p> int fact_back2_2 (callback_2 FN) <br/>{< br/> int cool = 5; <br/> return FN (cool ); <br/>}</P> <p> // ========================== ==============</P> <p> void main () <br/>{< br/> int * A; <br/> A = NULL; </P> <p> fact1 (5, & ); <br/> printf ("* A = % d/N", * A); <br/> A = NULL; </P> <p> int B; <br/> fact2 (5, B); <br/> printf ("B = % d/N", B); </P> <p> int C; <br/> C = fact_back2 (fact_back1); <br/> printf ("c = % d/N", c); </P> <p> int C_2; <br/> c_2 = fact_back2_2 (fact_back1_2); <br/> printf ("c_2 = % d/N", C_2); <br/>}
Reference is the simplest and most convenient.
Let's talk about the pointer. It turns out that the last parameter of many functions in d3d is a returned pointer and then used. It's strange. I tried it many times, but it was not easy to use today.
However, the above methods are better suited to some scenarios. Please give some advice to the cool ~ Thanks