Example Analysis of function pointer and callback function
Xiaobian to share with you the example analysis of function pointers and callback functions, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article. Let's learn about it!
one。 Function pointer
With regard to the concept of a function pointer, you can think of an integer pointer pointing to an integer whose value is the address of the object it points to; a string pointer pointing to a string whose value is the first address of the string pointed to; therefore, a function pointer is of course a pointer variable, it points to a function, and its value is the entry address of the function it points to.
Function pointers are defined as follows:
Typedef int data_type;data_type (* pfun) (data_type, data_type)
A function pointer pfun is defined in the above statement, which indicates that it points to a function with a return value of data_type and arguments of type data_type. The parentheses above the first parenthesis (* pfun) cannot be omitted, otherwise it will become:
Data_type * pfun (data_type, data_type)
In this way, you declare a function named pfun, and its return value is that the data_type*, parameter is two parameters of type data_type.
Here is a chestnut to illustrate the use of function pointers:
# include using namespace std;typedef int data_type;data_type add (data_type& a, data_type& b) {return (a + b);} int main () {data_type a = 2; data_type b = 3; data_type (* pfun) (data_type&, data_type&); pfun = & add; cout