The callback function of C + +

Source: Internet
Author: User

The callback function of C + +

Before understanding "callback functions", the concept of function pointers 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 running related objects need to be loaded into memory, which determines that any object that is run by the program can point to it 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 int Main (int argc,Char* argv[])2{3     printf (  "Hello world!\n"); 4     return 0 ; 5 }

It is then implemented in the form of a function call:

1 voidInvoke (Char*s);2 3 intMainintargcChar*argv[])4 {5Invoke ("Hello world!\n");6     return 0;7 }8 9 voidInvoke (Char*s)Ten { One printf (s); A}

The function pointer is used to implement:

1 voidInvoke (Char*s);2 3 intMain ()4 {5     void(*FP) (Char* s);//declaring a function pointer (FP)6Fp=invoke;//assigns the entry address of the Invoke function to the FP7fp"Hello world!\n");//function pointer FP implementation function call8     return 0;9 }Ten  One voidInvoke (Char*s) A { - printf (s); -}

It is known that the only difference between the declaration of a function pointer function is that the pointer name (*FP) is used instead of the function name invoke, so that this declares a pointer to the functions and then assigns a value fp=invoke the function pointer can be called. When declaring a function pointer, you can declare a function pointer as long as the function returns the value type, the number of arguments, the parameter type, and so on. Note that the function pointer must be enclosed in parentheses with void (*FP) (char* s).

In practice, for convenience, the function pointers are usually declared in a macro-defined way, with the following implementation:

1typedefvoid(*FP) (Char*s);2 voidInvoke (Char*s);3 4 intMainintargcChar*argv[])5 {6FP FP;//usually a macro FP is used to declare a function pointer fp7fp=Invoke;8fp"Hello world!\n");9     return 0;Ten } One  A voidInvoke (Char*s) - { - printf (s); the}
Array of function pointers

Here's a program to get a general idea of the array of function pointers:

1#include <iostream>2#include <string>3 using namespacestd;4 5typedefvoid(*FP) (Char*s);6 voidF1 (Char* s) {cout<<s;}7 voidF2 (Char* s) {cout<<s;}8 voidF3 (Char* s) {cout<<s;}9 Ten intMainintargcChar*argv[]) One { A     void* A[]={F1,F2,F3};//defines an array of pointers, where a is a normal pointer -a[0]("Hello world!\n");//compile error, pointer array cannot call function by subscript -  theFP 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 -      -     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, which is called a callback function. Simply put, it is the function that you implement when you run the function of someone else.

(2) Standard Hello World program:

1 int Main (int argc,Char* argv[])2{3     printf (  "Hello world!\n"); 4     return 0 ; 5 }

Modify it to a function callback style:

1 //Defining callback Functions2 voidPrintftext ()3 {4printf"Hello world!\n");5 }6 7 //defines the "calling function" that implements the callback function8 voidCallprintftext (void(*callfuct) ())9 {Ten callfuct (); One } A  - //implement a function callback in the main function - intMainintargcChar*argv[]) the { - Callprintftext (printftext); -     return 0; -}

Modify the callback style to take the parameter:

1 //defining a parameter callback function2 voidPrintftext (Char*s)3 {4 printf (s);5 }6 7 //defines the "calling function" that implements the parameter callback function8 voidCallprintftext (void(*callfuct) (Char*),Char*s)9 {Ten Callfuct (s); One } A  - //Implementing a function callback with parameters in the main function - intMainintargcChar*argv[]) the { -Callprintftext (Printftext,"Hello world!\n"); -     return 0; -}

At this point, you should have a general understanding of the callback function.

If there is a wrong place, very welcome to give guidance!

--"Thanks" data from http://www.cnblogs.com/chenyuming507950417/archive/2012/01/02/2310114.html

The callback function of C + +

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.