What is the use of callback functions?

Source: Internet
Author: User
In short, it is a function that is called. If you pass the pointer (address) of the function as an argument to another function, when the pointer is used to invoke the function it points to, we say this is

Why should I use it?
Because the caller can be separated from the callee. The caller does not care who the callee is, all it needs to know, but there is a called function that has a certain prototype, some restrictions (such as a return value of int).
If you want to know what the callback function does in practice, let's assume that there is a case where we're going to write a library that provides some
The implementation, such as
, quick sort, shell sort, shake sort, etc., but to make the library more generic, do not want to embed the sorting logic in the function, but let the user implement the corresponding logic, or, want to make the library available for a variety of
(int, float, string), what should I do now? can use
, and make callbacks.
Callbacks can be used to notify the mechanism, for example, sometimes to set a timer in the program, each time, the program will be notified, but the notification mechanism of the implementation of our program is ignorant. At this point, there is a need for a specific prototype
, use this pointer to make a callback to notify us that the program event has occurred. In fact, the SetTimer () API uses a callback function to notify the timer, and in the event that a callback function is not provided, it also sends a message to the program's

Another use of the callback mechanism
is Enumwindow (), which enumerates all the top-level windows on the screen, invokes a program-provided function for each window, and passes the window's handlers. If the callee returns a value, the iteration continues, otherwise, exits. Enumwindow () does not care where the callee is, nor does it care what the callee does with the handlers it passes, it only cares about the return value, because it will continue to execute or exit based on the return value.
In any case, the callback function continues from the C language, so in C + +, you should only use the callback function if you are establishing an interface with C code or dealing with an existing callback interface. In addition to the above, you should use virtual methods or function characters (functor) instead of callback functions in C + +.

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.