C + + callback function Introduction _c language

Source: Internet
Author: User

For many beginners, often feel that the callback function is very mysterious, very want to know how the callback function works. This article will explain what the callback functions are, what benefits they have, why they are used, and so on, and before you begin, assume you already know the function pointers.

What is a callback function?

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

Why should I use a callback function?

Because you can separate the caller from the callee. The caller does not care who the callee is, all it needs to know is that there is a called function that has a particular stereotype, 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's a situation 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, not embed the sort logic in the function, Let the user implement the appropriate logic, or do you want the library to be available for a variety of data types (int, float, string)? You can use the function pointer and make a callback.

Callbacks can be used to notify the mechanism, for example, sometimes to set a timer in the program, every time, the program will be notified, but the notification mechanism of the implementation of our program is ignorant. At this point, we need to have a specific prototype of the function pointer, with this pointer to the callback, to notify us that the program event has occurred. In fact, the SetTimer () API uses a callback function to notify the timer, and it sends a message to the program's message queue in case the callback function is not supplied.

Another API function that uses the callback mechanism is Enumwindow (), which enumerates all top-level windows on the screen, calls a program-supplied function for each window, and passes the window's handlers. If the caller returns a value, the iteration continues, otherwise, exit. Enumwindow () does not care where the caller is, nor does he care what the handler that the caller used to do with it, it cares only about the return value, because it will continue to execute or exit based on the return value.

Anyway, the callback function is continued from the C language, so in C + +, the callback function should be used only when establishing an interface with C code or dealing with an existing callback interface. In addition to the above, virtual methods or function characters (functor), rather than callback functions, should be used in C + +.

The following is a simple callback function written by yourself, which is easier to understand than the other complex code:

Copy Code code as follows:

#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 number \ n";
else printf ("%d is not the end \ n");
}
void Mycallback (void (*perfect) (int), int n)
{
Perfect (n);
}

int main ()
{
int n;
printf ("Please enter a positive integer \ n");
scanf ("%d", &n);

Mycallback (Perfect,n);
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.