Callback callback function

Source: Internet
Author: User

It is a function designed by you but called by the Windows system, collectively referred to as the callback function. Some API functions require callback as one of your parameters. Such as settimer, linedda, and enumobjects.

Callback functions are defined by developers according to a certain prototype (each callback function must follow this principle)

For example:
----------------------------------------
Bool callback dialogproc (

Hwnd hwnddlg, // handle of dialog box
Uint umsg, // message
Wparam, // first Message Parameter
Lparam // second Message Parameter
);
----------------------------------------
Note:

The callback function must have the keyword callback;
The callback function must be a global or static function and cannot be defined as a member function of a specific class.

2
The callback function is not directly called and executed by the developer (only the system interface API function is used as the starting point)
3
The callback function is usually passed to the system API as a parameter, which calls
4
The callback function may be called once by the system API or cyclically multiple times.

Call the callback function to demonstrate enumobjects and find that the GDI obect of a device context conforms to our form.

Suppose we have a cmycalss:

Class cmyclass {
PRIVATE:
Int ncount;
Int callback _ Export
Enumobjectsproc (lpstr lplogobject, lpstr lpdata );
Public:
Void enumit (CDC & DC );
}

Void cmyclass: enumit (CDC & DC)
{
// Callback function
DC. enumobjects (obj_brush, enumobjectsproc, null );
}

The actual code of the C ++ compiler for cmyclass: enumit is equivalent:

Void cmyclass: enumit (CDC & DC)
{
// Callback function
CDC: enumobjects (obj_brush, enumobjectsproc, null, (CDC *) & DC );
}

The last parameter you see is actually the this pointer. The member function of the class can use this pointer to capture the correct object information. ncount =
0; actually this-> ncount = 0;
Based on the same principle, since enumobjectproc in the above example is a member function, the C ++ compiler will also prepare a hidden parameter for it,
The callback function is used for Windows calls. If Windows does not call this function through any object, it does not need to pass this pointer to the callback function.
As a result, a random parameter in the stack will become the this pointer, and the result is of course a program crash.

Therefore, to use a function as the callback function, you must tell the C ++ compiler not to put the this pointer as the last parameter of the function. The two methods can do this,

1. Do not use a class member function (that is, use a global function) as the callback function.

2. Use the static member function, that is, add the static modifier before the function.

The first approach is equivalent to using the callback function in the C language, and the second approach is similar to the OO spirit. Further, C ++
The static function feature in is that even if the object has not been generated, the static member already exists (both functions and parameters). In other words, you can call the class before the object is generated.
A static function or a static variable of a class. That is to say, anything declared as static (whether a function or variable) is not combined with an object. They are part of a class.
Points, not an object

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.