What is a callback function)

Source: Internet
Author: User

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)

{

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.