Use of callback functions and function pointers in large programs

Source: Internet
Author: User

The first time I got in touch with function pointers in my project, I used to see them in books, and I almost never used them. I don't know how to use them. I 've learned about it today, the following is a summary:

First, the function pointer Declaration

Void (* p) (); // p is a pointer to a function, not a common pointer.

This function has no input parameter and the return value type is void.

The following describes the main functions.

void caller(void(*ptr)()){//do something}

Note that the parameter of the main function is a function pointer. That is to say, you must pass the name or address of another function to caller.

The callback function is defined below.

Void func () {// The callback function here is a common function}

See the implementation below

Int main (int argc, int char * argv []) {caller (func); // here, the function name of func (), that is, the function address, is passed to caller, then you can use the func function in caller. It depends on how your func is written !}

Okay. Let's see how these functions are used in large projects.


First declare a pointer type, which can point to a function pointer.



Typedef bool (CALLBACK * LoadLibraryExFailCallBack) (const std: wstring & path, int stage) declares a function pointer called LoadLibraryExFailCallBack, CALLBACK finds through macro definition that _ stdcall is a mechanism by the caller to clean up the memory.

With the type, you can define the pointer variable.

LoadLibraryExFailCallBack g_callBack = NULL;

Here, we use the LoadLibraryExFailCallBack type to define the variable g_callBack and initialize it to prevent pointer pointing out


The following is the declaration of the main function.

void __declspec InstallLoadLibraryExFailCallBack(LoadLibraryExFailCallBack callback);

Have you noticed the parameter of this function? Yes, this parameter is of the function pointer type and requires you to pass a function pointer in to work!

Below is the implementation of this function

void InstallLoadLibraryExFailCallBack(LoadLibraryExFailCallBack callback){    g_callBack = callback; }

The reason is that g_callBack is an interface left by the underlying code to the upper layer, indicating that you can implement what you want to implement in this way, however, you are not allowed to make any changes to the Code at the bottom layer. To do this, take advantage of the g_callBack function pointer. You can do anything with the address you give it, you let it go to the hospital, it is a doctor and a nurse, you let it go to school, it is a teacher and a student, in short, this thing has, everything can be achieved in the upper layer, then give the address g_callBack, and g_callBack will be able to work at the bottom layer!

Isn't that amazing!

The following shows how amazing it is.

First, define the callback function at the upper layer,

bool __stdcall LoadLibraryMsgCallBack(const std::wstring &path                                      int stage)

This function is used to implement the upper-layer functions. You can change the name and write another function to implement the functions you want to implement. Here we will not write the definition of the callback function, just write anything.

The following describes the main function.


InstallLoadLibraryExFailCallBack(LoadLibraryMsgCallBack)

Okay. Give the function name to the underlying function interface. Although the LoadLibraryMsgCallBack function can be used in the InstallLoadLibraryExFailCallBack function, only one conversion is performed here, and the upper-layer function address is passed to the underlying function pointer, when the underlying layer is used, you can call it at will. As long as you InstallLoadLibraryExFailCallBack, you can use g_callBack (path, stage) at the underlying layer.

If you want to define another callback function, write

void __stdcall ReadMsgCallBack(const std::wstring &path)

Okay, first InstallLoadLibraryExFailCallBack (ReadMsgCallBack); next, it indicates that the address of this function has been passed to the underlying g_callBack. Next we can go to the underlying g_callBack (path) in this way, the upper-layer function is called, is it a very powerful function pointer.

This article is from the "selling cute programmers" blog. For more information, please contact the author!

Related Article

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.