C/C ++ callback function

Source: Internet
Author: User
Tags what callback

For many beginners, they often find the callback function mysterious and want to know how the callback function works. This article will explain what callback functions are, what benefits they have, and why they are used. Before you start, let's assume that you have learned about function pointers.

What is a callback function?

In short, a callback function is a function called through a function pointer. If you pass the pointer (address) of a function as a parameter to another function, when this pointer is used to call the function to which it points, we will say this is a callback function.

Why should I use a callback function?

Because the caller and the called can be separated. The caller does not care who is called. All they need to know is that there is a called function with a specific prototype and certain restrictions (for example, the returned value is int.

If you want to know what the callback function has in practice, first assume that there is such a situation, we need to write a library, which provides some sortingAlgorithmSuch as Bubble sorting, quick sorting, shell sorting, and shake sorting. However, to make the database more generic, you do not want to embed the sorting logic in the function, let the user implement the corresponding logic; or, what should I do if I want the database to be used for multiple data types (INT, float, string? Function pointers can be used for callback.

Callback can be used in the notification mechanism, for exampleProgramWhen a timer is set, the program will be notified at a certain time, but the implementers of the notification mechanism do not know anything about our program. At this time, we need a function pointer of a specific prototype, which is used for callback to notify us that the program event has occurred. In fact, the settimer () API uses a callback function to notify the timer. In case a callback function is not provided, it sends a message to the Message Queue of the program.

Another API function using the callback mechanism is enumwindow (), which enumerates all the top-level windows on the screen, calls a function provided by a program for each window, and transmits the processing program of the window. If the caller returns a value, iteration continues. Otherwise, exit. Enumwindow () does not care about where the caller is or what the handler passed by the caller is. It only cares about the returned value because it will continue to execute or exit based on the returned value.

In any case, the callback function continues from the C language. Therefore, in C ++CodeThe callback function is used only when an interface is created or when dealing with an existing callback interface. In addition to the above cases, the virtual method or function operator should be used in C ++, rather than the callback function.

The following is a simple callback function written by myself. It is easier to understand than other complicated code:

# Include <stdio. h>
# Include <stdlib. h>
Void perfect (int n)
{
Int I = 1;
Int COUNT = 0;
For (I = 1; I <n; I ++)
{

If (0 = n % I)
{
Count + = I;
}
}
If (COUNT = N)
Printf ("% d is the final number \ n", N );
Else printf ("% d is not the final number \ n", N );
}
Void mycallback (void (* perfect) (INT), int N)
{
Perfect (N );
}

Int main ()
{
Int N;
Printf ("enter a positive integer \ n ");
Scanf ("% d", & N );

mycallback (perfect, N );
return 0;

}< BR style = "MSO-special-character: Line-break;">

Related Article

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.