c++ callback function

Reference to: 

http://blog.csdn.net/kkk0526/article/details/17122081

 

Callback

(1) Concept: The callback function, as the name suggests, is the user defines a function, the user implements the program content of this function, and then passes this function as a parameter to the function of others (or system), and the other person (or system) ) to be called at runtime. The function is implemented by you, but is called by someone else's (or system) function at runtime by passing parameters, which is called a callback function. Simply put, it is to call back the function you implemented during the execution of other functions.

//Define the callback function with parameters
void PrintfTextCallback(char* s)
{
  printf(s);
}

//Define the "calling function" that implements the callback function with parameters
void CallPrintfText(void(*callfuct)(char*), char* s)
{
  callfuct(s);
}

TEST(training, check2)
{
  CallPrintfText(PrintfTextCallback, "Hello World!\n");
}

 

1) The callback function is a kind of function pointer. Only by first understanding the function pointer can we understand the callback more thoroughly

2) In addition to understanding the pointer function, there are several keywords that are not well understood.

 

A.

The main application form of typedef

The main applications of typedef are as follows:

1) Define a new type name for the base data type.

2) Define concise type names for custom data types (structs, utilities, and enumerations).

3) Define concise type names for arrays.

4) Define concise names for pointers.

//You can define new names for function pointers, e.g.
typedef  int (*MyFUN)(int a,intb);
// where MyFUN represents the new name of the pointer-to-function type. E.g
typedef int (*MyFUN)(int a,intb);
int Max(int a,int b);
MyFUN pMyFun;// The original text here is MyFUN *pMyFun, the compilation is wrong, because the MyFUN type itself is a pointer type.
pMyFun= Max;

Refer to: 

http://www.cnblogs.com/seventhsaint/archive/2012/11/18/2805660.html

 

B.

__stdcall : Parameters are passed from right to left and pushed onto the stack, and the called function clears the stack. The function code generated by this specification is smaller than that of __cdecl , but when the function has a variable number of parameters, it is automatically converted to the __cdecl calling specification.

__cdecl is the abbreviation of C Declaration (declaration, declaration), which indicates the default function call method of C language: all parameters are pushed onto the stack from right to left, and these parameters are cleared by the caller, which is called manual stack clearing.

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326268323&siteId=291194637