function pointers and callback functions

Source: Internet
Author: User

First, the function pointer

1. The function pointer is a pointer variable that points to the function address. A running program (process) occupies a certain amount of space in memory. Processes include compiled program code and variables that need to be used. Therefore, the function in the program code is some Word value field, to get a function address, that is to get the starting address of these words value field.

2. Three types of function pointers:
function pointers to normal C functions---function pointers in the C language
Pointer to a C + + class static member function---C + +
Pointer to a C + + class non-static member function---C + +

    function Pointersthe nature of nature is alsofunction Address. The address representation of a class member function is not the same as the address of a normal C function. To represent a member function of a class example, it is not enough to have a function address, because each instance of the class has a non-static member function, and each instance occupies a certain amount of space in memory, soNon- static class member functions the address is made up of two parts, one is the instance address and the other is the function address. Instance Addresspassed by the implied this pointer. function Addressis the offset address of the function in the memory occupied by the instance, except that the address is obtained through the function name and does not need to be concerned about how much it offsets.

the difference between a static member function and a non-static member function:Static member functionsthe first instance does not have one such function. Therefore, the address of a static member function does not require an instance address, which means thatno this pointer. Its address is essentially a normal C functionthe address is the same., except when you define a function pointer.

The difference between the first and the third is that a function pointer to a member function of a class instance that points to a non-static member function requires an implicit argument:This pointer to the class instance. a function pointer to a normal C function needs to befunction Addresscan be both. These two types of function pointers are incompatible (not mutually assignable).


3, the function pointer definition:
pointers to normal functions:
Int (*pfun) (float, char, char) = NULL; C

pointers to class member functions:
Int (MyClass::* Pmember) (float, char, char) = NULL; C + +
Int (MyClass::* Pconstmember) (float, char,char) const = NULL; C + +

TODO: function pointer definition to static member function

Second, callback function

1. The callback function is a function called through a function pointer. If you pass the pointer (address) of a 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, using the callback function, you need:

    • Statement

    • Defined

    • Set trigger conditions: In your function type, you convert the name of your callback function to an address as a parameter for easy system invocation.

Note: When declaring and defining, the callback function is called by the system.

3, callback function: A programmer cannot display the called function, by passing the address of the callback function to the caller to implement the call. Different content is implemented through a unified interface.




This article is from the "Flower Open Shore" blog, please be sure to keep this source http://zxtong.blog.51cto.com/10697148/1783582

function pointers and callback functions

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.