function of the callback function

Source: Internet
Author: User
Tags what callback

  has never understood the function of the callback function, and here are some answers to the function of the callback function.  1. The callback function is a very useful and important concept. When an event occurs, the system or other function automatically calls a function that you define.  2. The callback function is equivalent to an interrupt handler, which is called automatically by the system when it meets the conditions you set. To do this, you need to do three things: 1, declaration, 2, definition, 3, setting the trigger condition is to convert your callback function name into an address as a parameter in your function, so that it can be called by the system.  3. The so-called callback function is defined by you in a certain form and written to implement the content, when an event occurs, and is called by the system or other functions. Using a callback function is actually when you call a function and pass the address of a function you write as an argument to that function. And that function, when needed, that is, when something happens, invokes the callback function with the function address that is passed in, and you can take advantage of this opportunity to process the message in the callback function or to complete a certain operation. The callback function can only be a global function, or a static function, because this function is only used in this class, so in order to maintain the integrity of the class, we use the static member function of the class to do the callback function.  4. For many beginners, it is often felt that the callback function is very mysterious, very much want to know how the callback function works. This article will explain what callback functions are, what benefits they have, why they are used, and so on, before you begin, suppose you already know the function pointers.    (1) What is a callback function? In short, a callback function is a function that is called through a function pointer. 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 a callback function.    (2) Why use a callback function? 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 implementations of some sort algorithms, such as bubble sort, quick sort, shell sort, shake sort, and so on, but to make the library more generic and not want to embed the sort logic in the function, And let the user implement the corresponding logic, or, you want the library to be used for a variety of data types (int, float, string), at this time, what to do?  You can use a function pointer and make a callback. 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, you need to have aA specific prototype of a function pointer, using this pointer to callback, to notify our program that the event has occurred.  In fact, the SetTimer () API uses a callback function to inform the timer, and in the event that a callback function is not provided, it also sends a message to the program's message queue. Another API function that uses a 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 + +.

Function of the callback function

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.