C + + Learning Notes (iv)--pointer (2) __c++

Source: Internet
Author: User

callback functions and function pointers

Looking at the code recently, people who write code like to use callback functions and function pointers. Always feel that the callback function and function pointers are very mysterious, so look up some information to share with you.

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 to use callback functions

Because using a callback function separates 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. The callback function is like an interrupt handler, which is invoked automatically when the system meets the criteria you set.

How to use callback functions

With the callback function, we need to do three things: Declare the definition setting trigger condition: In your function type, convert your callback function name to an address as an argument for system invocation.

Notice and define that the callback function is called by the system, so you can think of it as belonging to the Windows system, not as a member function of one of your classes.

A callback function is a function that a programmer cannot display a call to implement a call by passing the address of the callback function to the caller. Callback functions are necessary, and the callback function is appropriate when we want to implement different content through a unified interface.

Declaration of function pointers

With a preliminary understanding of the callback function, let's take a look at the function pointer. Because to implement the callback, you must first define the function pointer.

void (*) ()

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 parameters in the right circle bracket

To declare a type definition for a function pointer:

Typedef void (* PFV) ()

PFV is a function pointer to a function that does not have an input parameter and the return type is voie. Use this type definition name to hide the responsible function pointer syntax.

void (*p) ();

void Func ()

{

......

}

p = func;

The assignment of P can be different, but it must be a pointer to a function, and the parameter and return type are the same.

For example:

A small example of what you are now learning to sell

#include <iostream>

using namespace Std;

typedef void (*PF) ();

void Func ()

{

cout << "func" << Endl;

}

void caller (PF pf)

{

PF ();

}

int main ()

{

PF p = func;

Caller (p);

System ("pause");

return 0;

}

calling convention

In Visual C + +, you can add _cdecl,_stdcall or _pascal to a function type to represent the calling specification (default is _CDECL). The call specification affects the given function name produced by the compiler, the order in which the parameters are passed, the stack cleanup responsibility, and the parameter passing mechanism.

However, in the Win32 program, I have seen more callback, the macro defined in Windef.h,

#define CALLBACK __stdcall

It has been agreed that functions remove parameters from the stack before they return to the caller.

From

callback function

Http://hi.baidu.com/spidermanzy/blog/item/b25b00956469c6097bf48016.html

The concept of callback functions and hook functions

Http://zq2007.blog.hexun.com/9068988_d.html

declaring function pointers and implementing callbacks

http://www.vckbase.com/document/viewdoc/?id=195

Source Documents

 

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.