Previously, the callback function for C/A + + was not very understanding, and today we will learn the callback function in C + +.
Before you understand the callback function, the concept of the function pointer is discussed first.
function pointers:
1. Concept: A pointer is a variable that is used to point to a memory address. When a program runs, all and all of the running related things need to be loaded into memory, which determines that any object when the program is running can point to him with pointers. Functions are stored in memory code areas, they also have addresses, so you can also use pointers to access the function, the pointer to the function entry address is called a function pointer.
2. First look at a Hello World program:
1 #include <stdio.h>23int main ()4{5 printf ("Hello world!\n"); 6 return 0 ; 7 }
It is then implemented in the form of a function call:
1#include <stdio.h>2 3 voidTestFunc (Char*str)4 {5printf"%s\n", str);6 }7 8 intMain ()9 {TenTestFunc ("Hello world!"); One return 0; A}
The function pointer is used to implement:
1#include <stdio.h>2 3 voidTestFunc (Char*str)4 {5printf"%s\n", str);6 }7 8 intMain ()9 {Ten void(*FP) (Char*);//declaring a function pointer (FP) Onefp = TestFunc;//assigns the entry address of the TestFunc function to the FP Afp"Hello world!");//function pointer FP implementation function call - return 0; -}
The only difference between the declaration of a function pointer and the declaration of a function is that the pointer name (*FP) is used instead of the name TestFunc. After the function pointer is declared, the function pointer is assigned FP = TestFunc to call the function pointer. When declaring a function pointer, as long as the function returns the value type, the number of arguments, the parameter types are saved consistently. Note that the function pointer must be enclosed in parentheses: void (*FP) (char *) or void (*FP) (char *str).
In practice, for convenience, the function pointers are usually declared in a macro-defined way, as in the example program:
1#include <stdio.h>2 3typedefvoid(*FP) (Char*); 4 5 voidTestFunc (Char*str)6 {7printf"%s\n", str);8 }9 Ten intMain () One { AFP FP;//usually a macro FP is used to declare a function pointer fp -fp = TestFunc;//assigns the entry address of the TestFunc function to the FP -fp"Hello world!");//function pointer FP implementation function call the return 0; -}
Array of function pointers
The same program is used to explore the array of function pointers.
1#include <stdio.h>2 3typedefvoid(*FP) (Char*);4 voidF1 (Char*s) {printf (s);}5 voidF2 (Char*s) {printf (s);}6 voidF3 (Char*s) {printf (s);}7 8 intMain ()9 {Ten void* A[]={F1,F2,F3};//defines an array of pointers, where a is a normal pointer Onea[0]("Hello world!\n");//compile error, pointer array cannot call function by subscript A -FP F[]={F1,F2,F3};//defines an array of function pointers, where f is a function pointer -f[0]("Hello world!\n");//correctly, an array of function pointers can be used for indirect invocation of functions the - return 0; -}
callback function
1. Concept: The callback function, as the name implies, is the user to define a function, the user to implement the function of the program content, and then the function as a parameter into the other (or system) function, by the other (or system) function at run time to invoke the function. Functions are implemented by you, but are called by other (or system) functions at run time through parameter passing. This is called a callback function. Simply put, it is the function that you implement when you run the function of someone else.
2. Start by looking at the Hello World program.
1 #include <stdio.h>2 3 int{5 printf ("Hello world!\n"); 6 return 0; 7}
Modify him into a function callback:
1#include <stdio.h>2 3 //Defining callback Functions4 voidTestFunc ()5 {6printf"Hello world!\n");7 }8 9 //defines a "callback function" that implements a callback functionTen voidCalltestfunc (void(*Callfunc) ()) One { A Callfunc (); - } - the //implement a function callback in the main function - intMain () - { - Calltestfunc (TestFunc); + return 0; -}
Then look at the example of the callback function with parameters:
1#include <stdio.h>2 3 //defining a callback function with parameters4 voidTestFunc (Char*str)5 {6 printf (str);7 }8 9 //defines a "callback function" that implements a callback function with parametersTen voidCalltestfunc (void(* callfunc) (Char*),Char*str) One { A Callfunc (str); - } - the //implement a callback with a parameter function in the main function - intMain () - { -Calltestfunc (TestFunc,"Hello world!"); + return 0; -}
At this point, you should have a general understanding of function pointers and callback functions.
A description of the callback function in C + +