VC callback function and usage

Source: Internet
Author: User
Tags response code

VC callback function and usage

The callback function is an Event Response Program. Every message in windows can be understood as an event, and the event response code should be defined by the user. The user defines the event response code, but windows also needs to know the location of the Code (otherwise Windows does not know how to call it, which is useless ), the user needs to tell windows the pointer of the callback function. The most typical example is to assign the callback function pointer value to the lpfnwndproc component in the wndclass structure of the window class.
The parameter format of a callback function is defined by the caller of the callback function (generally in Windows), and the implementer of the callback function must follow this format. Windows programs are based on the event-driven model. Therefore, callback functions must be used.
For more information about callback functions, see SDK samples. The message ing mechanism in MFC has hidden the callback function of the window message response, which is also in line with the c ++ programming philosophy. The callback function is a global function after all, it cannot be implemented in the class, and the message ing mechanism aims to encapsulate the message response code in the window class (subclass of the cwnd class.

If you have time, check the message_map macro. Message ing is a callback function, but the usage of this callback function is different. A common callback function requires you to provide an address and upload it to a function for calling. However, the message ing function is defined by you and the message_map macro gets the address, and implement its call.

A callback function is a function that a programmer cannot explicitly call. It is called by passing the callback function address to the caller. To implement callback, you must first define the function pointer. Although the defined syntax is a bit incredible, if you are familiar with the general method of function declaration, you will find that the declaration of function pointers is very similar to that of function declaration.

Code:# Include "stdafx. H"
# Include "stdio. H" void (* fun1 )();
Void ()
{
Printf ("fun1/N ");
} Void caller (void (* fun1 )())
{
Printf ("fun1 start/N ");
Fun1 ();
Printf ("fun1 end/N ");
} Bool func2 (int * I)
{
Printf ("from func2 () = % d, hello World! /N ", (* I) ++ );
Return true;
} Void caller2 (bool func2 (int *), int * j)
{
Func2 (j );
} Int main (INT argc, char * argv [])
{
Printf ("from main (), hello World! /N ");
Printf ("/N ");
 
Caller (a); int I = 0;
For (Int J = 0; j <10; j ++)
{
Caller2 (func2, & I); // * pass the function address to the caller */
}
 
Getchar ();
 
Return 0;
}

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.