declaring function pointers and implementing callbacks

Source: Internet
Author: User

Programmers often need to implement callbacks. This article discusses the basic principles of function pointers and explains how to use function pointers to implement callbacks. Note that this is an ordinary function, excluding class member functions that are completely dependent on different syntactic and semantic rules (class member pointers will be discussed in another article).

declaring function pointers

A callback function is a function that the programmer cannot explicitly call, and the call is implemented by passing the address of the callback function to the caller. To implement a callback, you must first define the function pointer. Although the syntax of the definition is a little weird, if you are familiar with the general method of function declaration, you will find that the declaration of the function pointer is very similar to the function declaration. Take a look at the following example:

void f ();//function prototype

The statement above declares a function that has no input parameters and returns void. Then the function pointer is declared as follows:

void (*) ();

Let's analyze that the asterisk in the left circle bracket is the key to the function pointer declaration. The other two elements are the return type (void) of the function and the entry parameter in the side circle bracket (in this case, the argument is empty). Note that the pointer variable has not yet been created in this case-just declaring the variable type. You can now use this variable type to create a type definition name and to use the sizeof expression to get the size of the function pointer:

To get the size of a function pointer

unsigned psize = sizeof (void (*) ());

Declaring a type definition for a function pointer

typedef void (*PFV) ();

PFV is a function pointer, which points to a function that has no input parameters and returns a class behavior void. Use this type definition name to hide complex function pointer syntax.

The pointer variable should have a variable name:

void (*p) (); P is a pointer to a function

P is a pointer to a function that has no input parameters and the type of the return value is void. The asterisk in the left circle bracket is the pointer variable name. You can assign a value with a pointer variable, and the content of the value is a signature-matching function name and return type. For example:

void Func ()

{

* Do something * *

}

p = func;

The assignment of P can be different, but it must be the address of the function, and the signature and return type are the same.

Pass the address of the callback function to the caller

You can now pass p to another function (caller)-caller (), which calls the function that P points to, and the name of the functor is unknown:

void caller (void (*PTR) ())

{

PTR (); /* Call PTR-pointing function * *

}

void Func ();

int main ()

{

p = func;

Caller (p); /* Transfer function Address to caller * *

}

If different values are assigned to P (different function addresses), then the caller will invoke the function of the different addresses. The assignment can occur at run time, so that you can implement dynamic binding.

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.