Talking about the callback function of C++

Reference example: https://blog.csdn.net/yidu_fanchen/article/details/80513359

Speaking of callback functions are often associated with callback function pointers ; the question is how do we declare the callback function, how to execute the callback function, and how to use the callback function pointer? What is the connection between them? Before answering these questions, understand what a callback function is?

Callback function : The program passes the function pointer of this function to other functions through parameters. Calling this function pointer in that function is equivalent to calling this function. Such a process is called a callback, and the called function

Standard definition : typedef  void  (*SCT_XXX)(LPVOID lp, const CBParamStruct& cbNode);

One, the declaration of the callback function

The pointer of the function type needs to be declared before using the function, including the type of the function itself, the type of the parameter, etc

 

1: Declare the function, 2: Define the function pointer, 3: Define the carrier function of the callback function

  • Use of callback function

 

1: Create the class to which the callback function belongs; 2: Implement the callback function 3: Initialize SetParam

The following shows the specific implementation of the SetParam() function, as follows:

 

It is not difficult to see from the above figure that the function pointer is actually passed in the SetParam function, which means that the address of the RecvCallBack function is obtained through the pCallbackFun parameter in SetParam;

Four, summary:

The above is an explanation of the relationship between the callback function and the callback function pointer; the essence of the callback is the transfer of the function pointer. Understanding the callback mechanism and understanding the function pointer are the prerequisites for its flexible use;

Reference example: https://blog.csdn.net/yjhdxflqm/article/details/50482119

Principle: https://blog.csdn.net/sinat_38183777/article/details/83958887  

Thanks to the blogger: once mortal

Welcome to follow the editor's WeChat public account "IT Rapeseed U"

Guess you like

Origin blog.csdn.net/xiaoshunzi111/article/details/105999440