A preliminary study of C + + callback function

Source: Internet
Author: User
Tags int size message queue sort

Brief introduction

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 + +.

A simple callback function implementation

The following creates a Sort.dll dynamic-link library that exports a type named comparefunction--typedef int (__stdcall *comparefunction) (const byte*, const byte*) , it is the type of callback function. In addition, it also derives two methods: Bubblesort () and quicksort (), which have the same prototype, but implement different sorting algorithms.

void DLLDIR __stdcall Bubblesort(byte* array,int size,int elem_size,CompareFunction cmpFunc);
void DLLDIR __stdcall Quicksort(byte* array,int size,int elem_size,CompareFunction cmpFunc);

These two functions accept the following parameters:

byte * Array: A pointer to an array of elements (any type).

int Size: The number of elements in an array.

int elem_size: The size, in bytes, of an element in an array.

· Comparefunction Cmpfunc: A pointer to a callback function with the above prototype.

The two functions are sorted by array, but each time you decide which of the two elements is in front, and the function has a callback function, the address is passed in as a parameter. For the writer, it is not necessary to mind where the function is implemented, or how it is implemented, but only the two addresses of the elements used for comparison, and returns a value (which both the creator and the user of the library must adhere to):

-1: If the first element is smaller, it should precede the second element in the sorted array.

• 0: If two elements are equal, their relative position is not important, and in the sorted array, it doesn't matter who is in the front.

• 1: If the first element is larger, then in the sorted array, it should be followed by the second element.

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.