Help:CallBack function
-
What is the CallBack function? How to use a CallBack function? When to use? Can someone help me? I am a beginner. curiosity
-
What is the CallBack function? How to use a CallBack function? When to use? Can someone help me? I am a beginner. curiosity
curiosity5374 wrote: What is the CallBack function? A user supplied function. (Example: WndProc) curiosity5374 wrote: How to use a CallBack function? A callback (function pointer) is usualy passed as an argument to anouther function, so that it can be called back to process some of the information required for that function. Examples: 1) qsort(elem1,elem2,compare) where compare is the callback function. 2) SetTimer(hWnd,nIDEvent,uElapse,lpTimerFunc) where lpTimerFunc (timer procedure) is the callback function. curiosity5374 wrote: When to use? When a function you are calling requires it, or when a structure (or class) requires it as a member variable (normaly passed as argument to some other function). INTP
-
curiosity5374 wrote: What is the CallBack function? A user supplied function. (Example: WndProc) curiosity5374 wrote: How to use a CallBack function? A callback (function pointer) is usualy passed as an argument to anouther function, so that it can be called back to process some of the information required for that function. Examples: 1) qsort(elem1,elem2,compare) where compare is the callback function. 2) SetTimer(hWnd,nIDEvent,uElapse,lpTimerFunc) where lpTimerFunc (timer procedure) is the callback function. curiosity5374 wrote: When to use? When a function you are calling requires it, or when a structure (or class) requires it as a member variable (normaly passed as argument to some other function). INTP
Nice answer but I would like to add something to 'when to use' Callback function is used to get events without having to keep on polling them or keep waiting for them in a loop For example lpTimerFunc mentioned above will be called when the time is elapsed,the program need not bother about it in meantime and continue to do other tasks
-
Nice answer but I would like to add something to 'when to use' Callback function is used to get events without having to keep on polling them or keep waiting for them in a loop For example lpTimerFunc mentioned above will be called when the time is elapsed,the program need not bother about it in meantime and continue to do other tasks
How about: When your program is written in C instead of C++. In 'C', since you want to allow the 'client' to override some basic functionality, you need to provide a callback - the client provides the pointer to his function that will do the special processing. In 'C++' you can just provide a stub virtual fuction in a base class and the client can create a derived class and override the virtual function to do something special for his purposes. Sounds like a good test question...