Callback function theory
Calling (calling) mechanisms have been in the times from the compilation of extensive use of: preparing a ready-made code, the caller can always jump to this section of code, starting address, then return to jump when you perform a follow-up address. CPU prepared a ready-made call command, the call protection when stacking the scene, calling the pop-up after the site address from the stack to automatically return. Through the stack to protect the scene, it was transferred to the caller and can not know each other, then have a later function and components.
The call mechanism is not perfect. Callback function is an example. For example, write a quick sort function for others to call, which will contain relatively small. Trouble comes: At this point, and I do not know what to compare that kind of data - integers, floating point numbers, strings? So he had to for each type of data produced a different sort function. More common approach is to set out the parameters in the function of a callback function address, and inform the caller: Jun required to prepare a comparison function itself, which contains two pointer type parameters, the function pointer to compare the two referred to the size of the data, and Note by the function returns the value of the comparison. Sort function to call a function provided to compare the size of a pointer by passing parameters, you can totally regardless of the type of data being compared. Callee caller call back function (enough mouthpiece), the so called callback (callback).
Another Windows system also contains a wider range of callback mechanisms that the message mechanism. The basic message of this is the Windows control means, at first glance has nothing to do with the function call, is actually a disguised form of function calls. The purpose is to send a message to inform debit
Run a pre-prepared code, the equivalent of calling a function. News and LParam WParam attached to the equivalent of function parameters, but is more common than the average number of parameters. Application can take the initiative to send a message, more cases are waiting for Windows to send a message. Once the message into their message queues, they seized those interested, jump to the implementation of the appropriate message handling code. Operating system services for the application of this is, by the application to call. The application, once started, have to turn the call waiting for the operating system. This is obviously also a callback, or a generalized callback. In fact, the application can be formed between such a callback. If process A process B receive a message sent to start a piece of code, which Youxiang process A to send a message, which formed a callback. This callback is covert, Nongbu Hao will mess recursive call, if the lack of termination conditions will be endless loop until the process ruin. If deliberately written into the recursive call, and set conditions for a good end, is actually very interesting. However, the structure of this program is too hidden, unless it is necessary, or not at all.
Use of information can constitute a narrow callback. One example cited above sorting function, the callback function address can be replaced by the window handle. So, when the need to compare the data size, not to call the callback function, but by the SendMessage API function to send a message to the specified window. Comparison of the data received messages will be responsible for the size, the results of the comparison by the return value of the message itself passed the message sender. The realization of the function and the callback function is no different. Of course, this example is purely to draw feet on the snake news, sculptors the program was very slow. But other cases are not always the case, in particular the need asynchronous calls, sending messages is a good choice. If the callback function contains the file handle of the class of low-speed processing, such as the caller may not need to synchronize calls to the asynchronous call to start a separate thread, and then immediately implement follow-up code, the other to do things slowly to thread . An alternative approach is to use API function PostMessage to send an asynchronous message, and then immediately follow up the implementation of the code. This thread save trouble than many of their Gao Ge, and more secure.
Today we are living in a time object. As long as with the programming, whatever can not be separated object. But the object does not remove the callback, but it carried forward all over the place, but mostly in the incident (event) appeared as embedded in a fabric, seem more orthodox, more easily accepted. Application to use a component, first find out the total component properties, methods, and events, and then to the component property assignment, at the appropriate time call the appropriate method components, but also to write the event handling routines to prepare for component code to call. What event? It is merely a pointer to the address of the event routine, and a callback function addresses makes no difference. However, this callback method to be wise than many of the traditional callback function. First of all, it is to make people feel uncomfortable in the callback function into a natural handling routines, so that programmers Dunjue smoother. Furthermore, the address is a dangerous thing, make good use of the accelerated procedure, a trap with bad everywhere, the program to crash. Always thinking of modern programmatically hide the address (such as VB hides a more thorough and Java), the cost of the program is to reduce efficiency. Event routine to programmers without direct operation of address, but does not make programs slow down.
Custom callback function
Callback function is not explicitly call the function; by the address of the callback function passed to the caller in order to achieve call. Callback function to use is necessary, we want a unified interface different content, then go back out function is very appropriate. For example, several different devices we were writing a different display function: void TVshow (); void ComputerShow (); void NoteBookShow ()... and so on. This is what we want to use a single display function, then we can go back out the function of the.
void show (void (* ptr )());
When used according to different parameters of incoming call different callback function.
Different programming languages may have different syntax, the following give a callback function in c language example, one of the callback function with no arguments, another callback function arguments.
/ / Test.c
# Include <stdlib.h>
# Include <stdio.h>
int Test1 ()
for (i = 0; i <30; i + +)
printf ("The% d th charactor is:% c \ n", i, (char) ('a' + i% 26));
int Test2 (int num)
for (i = 0; i <num; i + +)
printf ("The% d th charactor is:% c \ n", i, (char) ('a' + i% 26));
void Caller1 (void (* ptr )())// pointer to a function for function parameters
(* Ptr) ();
void Caller2 (int n, int (* ptr )())// pointer to a function for function parameters, where the first parameter is a pointer to a function of services,
(/ / Can not write void Caller2 (int (* ptr) (int n)), so the definition of syntax errors.
(* Ptr) (n);
int main ()
printf ("************************ \ n ");
Caller1 (Test1); / / equivalent to calling Test2 ();
printf ("&&&&&&************************ \ n ");
Caller2 (30, Test2); / / equivalent to calling Test2 (30);
Above address by the callback function passed to the caller in order to achieve call, but should be noted that the use of a callback function with parameter. To achieve the callback, you must first define the function pointer. Function pointers defined here a little mention. For example: int (* ptr) (); where ptr is a function pointer, which (* ptr) in the brackets can not be omitted, because parentheses take precedence over star, as has become a return type for the integer function declaration the.
Note: The fox that the callback function with multi functions of the state function is most important is to provide a dynamic embed code means, to achieve a way to function within the disruption.
This article comes from CSDN blog, reproduced, please indicate the source: http://blog.csdn.net/woyaowenzi/archive/2009/03/02/3950116.aspx