C + + callback function

Source: Internet
Author: User

Before starting the callback function let's briefly introduce the function pointer

Assume that there is a declaration of the following function

int foo (int);

The definition of a function pointer:

Int (*p) (int) =&foo;

The above & characters can be omitted, the compiler will convert the function name into a function pointer, plus & just show the description of the compiler will perform the task implicitly

Use of function pointers:

(*p) (10);

P (10);

Both of the above are called by function pointers to the Foo function, and the month can be called by using the function name directly, foo (10);


The following is a formal introduction to callback functions:


The most essential features of callback include two points: registration and triggering

Concept: A callback function is a function you write yourself, you need to call an intermediate function (which can be a system function or write it yourself) and pass a function pointer to the function, and the function pointer points to that callback function, so that you can do something in that callback function.

A simple example:

int Compare_char (void const *a,void const *b) {//do something}int compare_int (void const *a,void const *b) {//do som        Ething}node *search_list (Node *node,void const *value,int (*compare) (void const *,void const*)) {while (node!=null) {        if (0==compare (Value,&node->_value)) break;    node=node->_next; } return node;
The function calls the following statement

Search_list (&node,&value,compare_int);


In this example, the callback function compares two values, returns 0 for equality, not 0 for inequality, and the lookup function is independent of the type, and the real comparison is done by the comparison function


The concept of layering is used to explain the callback mechanism: the main function and the comparison function belong to layer A, the Search_list function belongs to the B layer, the main function calls the search_list,search_list and the comparison function is called, and the Search_list function is equivalent to an interface.


how can a class member function be used as a callback function in C + +, must it be a static method?

must beStatic member functionsorGlobal Functionsto implement the callback function
Presumably the reason is that the normal C + + member function implies a transfer function as an argument, the this pointer, C + + by passing the this pointer to the member function so that the function can access the data members of C + +. Because of the reason of this pointer, an ordinary member function as a callback function isimplied this pointer problem causes the function parameter number to not match, which causes the callback function to compile unsuccessfully.


This article is from the "Zero Egg" blog, please be sure to keep this source http://lingdandan.blog.51cto.com/10697032/1770925

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