In-depth VC callback function of the use of detailed _c language

Source: Internet
Author: User
Tags response code
callback function plainly is the event responder, each message in Windows can be interpreted as an event, and the response code for the event is defined by the user himself. The user defines the code for the event response, but Windows knows the location of the code (or Windows doesn't know how to call it, which is useless), so the user needs to tell Windows about the callback function, the most typical example being the structure of the window class (WNDCLASS) Assigns a callback function pointer value to the LPFNWNDPROC component.

The parameter format of the callback function is defined by the caller of the callback function (typically Windows), and the implementation of the callback function must follow this format. Windows programs are based on an event-driven model, which is necessarily a mechanism that uses callback functions.

To get a thorough understanding of callback functions, take a look at the SDK Samples. The message mapping mechanism in MFC has hidden the callback function of the window message response. This is also in line with C + + programming thinking that the callback function is ultimately a global function that cannot be implemented in the class, and the message mapping mechanism is designed to eventually encapsulate the message response code in the window class (the subclass of the CWnd Class).

If you have time, look at the MESSAGE_MAP macro, the message map is a callback function, but the use of this callback function is different. The normal callback function is to give you an address, a function to call, and a message mapping function that you define a function by Message_map macros to get the address and implement its invocation.

A callback function is a function that the programmer cannot explicitly call, and the call is implemented by passing the address of the callback function to the caller. To implement a callback, you must first define the function pointer. Although the syntax of the definition is a little weird, if you are familiar with the general method of function declaration, you will find that the declaration of the function pointer is very similar to the function declaration.
CODE:
Copy Code code as follows:

#include "stdafx.h"
#include "stdio.h"
void (*FUN1) ();
void A ()
{
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 < J + +)
{
Caller2 (Func2, &i); * Transfer function Address to 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.