“This is the 25th day of my participation in the August Gwen Challenge.

A function pointer

concept

A pointer is a variable that points to a memory address. When a program is running, all run-time objects need to be loaded into memory, which means that any object that a program is running can point to it with a pointer. Functions are stored in the code area of memory, they also have addresses, so you can also use Pointers to access the function, the pointer to the address of the function entry is called the function pointer.

Hello World program

int _tmain(int argc, _TCHAR* argv[]) { printf("Hello World! \n"); return 0; }Copy the code

Using function calls:

void Invoke(char* s) { printf(s); } int _tmain(int argc, _TCHAR* argv[]) { Invoke("Hello World! \n"); return 0; }Copy the code

Use function pointer to implement:

typedef void (* PINVOKE)(char *s); void Invoke(char* s) { printf(s); } int _tmain(int argc, _TCHAR* argv[]) { PINVOKE fp = Invoke; fp("Hello World! \n"); return 0; }Copy the code

Fp =Invoke; fp=Invoke; fp=Invoke; fp=Invoke; fp=Invoke; When declaring a function pointer, you can declare a function pointer as long as the return value type, parameter number, and parameter type of the function are consistent. Note that function Pointers must be enclosed in parentheses void (fp)(char s).

The callback function

concept

Callback function, as the name implies, is a function defined by the user, the user implements the program content of this function, and then passes this function as a parameter in the function of others (or system), which is called by others (or system) function at runtime. A function that you implement but that someone else’s (or system’s) function calls at run time with arguments is called as a callback function. In simple terms, it is the function you implement that is called back and forth by someone else’s function while it is running.

The sample

typedef void (*CALLFUN)( char* ); void CallPrintfText(CALLFUN fp, char* s) { fp(s); } void PrintfText(char* s) { printf(s); } int _tmain(int argc, _TCHAR* argv[]) { CallPrintfText(PrintfText, "Hello World! \n"); return 0; }Copy the code

Class member functions as callback functions

Callbacks are Windows SDK technology based on C, not C++. Programmers can use a C function as a callback directly, but attempting to use a C++ member function as a callback will cause an error. Because ordinary C++ member functions have an implicit pass function as a parameter, the “this” pointer, C++ makes the program function access C++ data members by passing a pointer to itself to the member function.

So there are two solutions to using a member function of a class as a callback: no member functions (the C function that uses the friend operator to access the data members of the class); Using static member functions: The following example (this example addresses the statically only access restriction of static member functions)

#include "stdafx.h" #include <iostream> using namespace std; class CPrintString { public: CPrintString(){}; ~CPrintString(){}; public: void PrintfText(const char* s){ printf(s); }; static void StaticPrintText(void* ptClass, const char* s); }; void CPrintString::StaticPrintText(void* ptClass, const char* s) { CPrintString* ptThis = static_cast<CPrintString*>(ptClass); if (NULL == ptClass) { return; } ptThis->PrintfText(s); } typedef void (*PPRINTTEXT)(void* ptClass, const char* s); void CallPrintText(void* ptClass, const char *s, PPRINTTEXT fp) { fp(ptClass, s); } int _tmain(int argc, _TCHAR* argv[]) { CPrintString obj; CallPrintText((void*)&obj, "Hello World! \n", CPrintString::StaticPrintText); return 0; }Copy the code