C ++ callback function

Source: Internet
Author: User

The callback function is like an interrupt processing function, which is automatically called when the system meets your Preset conditions. To do this, you need to do three things:

1. Statement;

2. Definition;

3. Set the trigger condition to convert your callback function name into an address as a parameter in your function for system calling.


Declaration is to declare a function pointer;


The definition is to implement the callback function;


The trigger condition is that the defined function is assigned a pointer to a callback function. When called, the pointer is used to call the function with the parameter;


1) Common callback Functions


(1) function pointer
Callback is implemented by using the function pointer in the C language. The callback is implemented by passing the address of the callback function to the called function. Therefore, to implement callback, you must first define the function pointer. See the following example:

Void func (char * s); // function prototype
Void (* pfunc) (char *); // function pointer

We can see that the definition of a function is very similar to that of a function pointer.

In general, to simplify the definition of variables of the function pointer type and improve the readability of the program, we need to customize the function pointer type.

Typedef void (* PCB) (char *);

A callback function can be called by a program like a normal function, but it can be called a callback function only when it is passed as a parameter to the called function.

Example of the called function:

Void getcallback (PCB callback)
{
/* Do something */
}
When calling the above function, you need to implement a PCB type callback function by yourself:
Void fcallback (char * s)
{
/* Do something */
}
Then, you can directly pass fcallback to getcallback as a variable,
Getcallback (fcallback );

If different values are assigned to this parameter, the caller will call functions of different addresses. Assign values can occur at runtime, so that you can achieve dynamic binding.


2) The callback function is used as a function parameter.

This method is widely used in SDK encapsulation, such as the sdks of haikang and Dahua;


For example, haikang's DVR preview SDK:

Net_dvr_api long _ stdcall encode (long luserid, lpnet_dvr_clientinfo lpclientinfo, void (callback * frealdatacallback_v30) (long lrealhandle, DWORD dwdatatype, byte * pbuffer, DWORD
Dwbufsize, void * puser) = null, void * puser = NULL, bool bblocked = false );


Net_dvr_clientinfo clientinfo;
Clientinfo. hplaywnd = getdlgitem (idc_static_play)-> m_hwnd;
Clientinfo. lchannel = m_strudeviceinfo.struchaninfo [m_icurchanindex]. ichanindex;
Clientinfo. llinkmode = 0;
Clientinfo. smulticastip = NULL;
Trace ("channel number: % d \ n", clientinfo. lchannel );
M_lplayhandle = net_dvr_realplay_v30 (m_strudeviceinfo.lloginid, & clientinfo, null, null, true );



3) if the callback function does not need to be used as a function parameter, you can also use an init function to pass the callback function to the member variables of the class, and then use the member variables in the class for callback.

Related Article

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.