Tune function (data collection)

xiaoxiao2021-03-06  79

You provide a function of the address to a functional module. Then, when this functional module completes its work, it calls this function. This is the callback function, and there is no difference between the general function.

The callback is implemented by a function pointer in the C language. By transmitting the address of the callback function to the modified function to achieve the callback. Therefore, to achieve a callback, you must first define a function pointer, please see the example below:

Void func (char * s); // Function prototype VOID (* pfunc) (char *); // function pointer

It can be seen that the definition of the function is very similar to the definition of the function pointer.

Generalization, in order to simplify the variable definition of the function pointer type, improve the readability of the program, we need to customize the function pointer type.

TypeDef void (* pcb) (char *);

The callback function can be called like a normal function, but only it is called a callback function when it is passed as a parameter to the called function.

Example of the modulous function:

Void getCallback (PCB Callback) {/ * do Something * /} User When calling the above function, you need to implement a PCB type callback function: void fcallback (char * s) {/ * do something * /} then, You can pass the FCallback directly as a variable to getCallback, getCallback (fcallback);

If the different values ​​are assigned to this parameter, the caller will call a function of different addresses. The assignment can occur when running, so that you can realize dynamic binding.

转载请注明原文地址:https://www.9cbs.com/read-120788.html

New Post(0)