Http://www.yuanma.org/data/2006/0605/article_641.htm
I was confused when I first started to contact callback. when many people explain this problem, they always use APIs for example. Originally, cainiao was most afraid of API, ^_^. callback is not necessarily related to the API.
In fact, callback is a process of using a function pointer to call a function.
Why callback? For example, I want to write a sub-module for you to receive commands sent from remote socket. after receiving the command, I need to call the function of your main module for corresponding processing. but I don't know which function you want to use to process this command, and I don't know what your main module is. CPP or. h, or, I don't have to worry about how you handle it in the main module, or what functions should be used to process it ...... what should I do?
Use callback.
In my module, I first define the callback function type and the callback function pointer.
Typedef void (callback * cbksenddomaintomain) (ansistring scmd );
Cbksenddomaintomain senddomaintomain;
In this way, senddomaintomain is a function pointer pointing to an ansistring parameter with a return value of void.
In this way, you can call this function when I receive the command.
...
Senddomaintomain (scommand );
...
But this is not enough. I have to give an interface function (such as init) so that you can call init in the main module to register this callback function.
In your main module, this may happen.
Void callback yoursendcmdfun (ansistring scmd); // declare
...
Void callback yoursendcmdfun (ansistring scmd); // define
{
Showmessage (scmd );
}
...
Call the init function to register a callback with my module. This may be the case:
Init (yoursendcmdfun ,...);
In this way, the expected goal is achieved.
Note that the callback function must be declared as global. If you want to use the callback function in the class, you need to add static before, which is actually equivalent to global.
Example
Declaration: typedef void (callback * lpshowfileopinfocbk) (lpvoid, const cstring & strpathname );
Parameter: lpshowfileopinfocbk lpcallback = NULL
Definition: void cxxdlg: showfileopinfocbk (lpvoid, const cstring & strpathname)
{
}