Use of callback Functions (CallBack) in C + +

Source: Internet
Author: User

If you try to use the member function of C + + directly ascallback functionAn error will occur, and even compilation will not pass.
The error is that ordinary C + + member functions imply aThe recursive function acts as a parameter, that is, the "This" pointer, and C + + accesses the C + + data member by passing the this pointer to its member function to implement the member function. This also allows you to understand why the C + + class Multiple instances can share member functions but-there are different data members. Because of the function of this pointer, a CALL-BACK member function is installed as a callback function this pointer does not match the number of function arguments, causing the callback function to fail to install. The key to solving this problem is to not let the this pointer work, by using the following two typical techniques can be solve the problems encountered in using callback functions in C + +. This method is versatile and suitable for any C + +.       
1).    does not use member functions, in order to access member variables of the class, you can make the user-friendly meta-operator (friend), in C + +, describe the function as a friend of the class.       
2) .   uses a static member function, and the static member function does not use the this pointer as an implicit argument, so it can be used as a callback function. The static member function has two major characteristics: first, you can class pointer as a class member by initializing the static pointer when the class is created, such as Pthis=this, All member variables and member functions are then accessed through the static pointer in the callback function.

This approach applies to cases where there is only one class instance, because multiple class instances share static class members and static member functions, which results in a static pointer pointing to the last class instance created. To avoid this situation, you can use a parameter of the callback function to pass the this pointer, which enables data member sharing. This is a bit of a hassle, so don't repeat it here. for static methods accessing non-static variables and functions, see http://www.cnblogs.com/this-543273659/archive/2011/08/29/2157966.html)

First understand what a callback function is: for example, if the function void Callbackf (int n) {} is to be used as a callback function, CALLBACKF must appear as a formal parameter of the keynote function, such as void f (void (*p (int)), int n).

/////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////

What do I do if I want to invoke a member of a class in static?
I have to define a global pointer to the class, and then assign the this pointer of the class to the global pointer in the class's initialization function.
The other members of the class are then called through the global pointer in the static function.

Example 1:

#include"StdAfx.h"#include<iostream>#include<Assert.h>Using NamespaceStdClasstest{Public: FriendvoidCallbackfun (void) {cout<< "CallBack function! ";} //friend function as a callback function friend way implementation, // };typedef void (*fptr) (void); void fun (fptr ptr) {ptr ();} int main (void) {Fun (callbackfun); return 0;} 

Example 2:

#include <iostream> using namespace std; Class A {public:static void callback ()//member function of class is implemented as callback function static method {cout<< "callback function started!  "<<endl; } }; void f (void (*p) ()) {p ();

} int main () {void (*p) ();  P=a::callback;  f (P); return 0; }

You can also set the F () function as a member function of a class:

#include <iostream> using namespace std; Class A {public:static void callback ()//member function of class is implemented as callback function static method {cout<< "callback function started!  "<<endl; } void F (void (*p) ()) {p ();

} };

int main () {a A;  void (*p) ();  P=a::callback;  A.F (P); return 0; }

What is a callback function
Callback functions are those that are written by themselves, but are not self-tuned, but are called functions to others. The message response function can be thought of as a callback function, because it allows the system to be called at the appropriate time. This is the message response function is to deal with the message, so we take out a single class. is essentially a callback function. But the callback function is not only a message response function, such as in kernel programming, the driver will provide some callback functions, when the data read and write of a device, let the system call these callback functions to perform some follow-up work. The callback function gives the programmer the ability to write code that jumps out of the normal program flow and adapts to the specific runtime to execute at the right time.
////////////////////////////////////////////
The normal function is: our function calls the function of the system,
Think of the program you wrote and the system already encapsulated function as two parts of your program using the system functions that call system functions using your program functions called callbacks are generally used for system functions and your functions to be asynchronous processing such as key events, Actually, it's a message. Your function is much earlier than the key event, so you have to submit this function as a callback function to the system, and then the system will call your function after it receives the key event.
/////////////////////////////////////////////
For example: void Fun () {printf ();} And the callback function is: The system calls your function. Win32 programming of the Wndproc,java event, C # delegate is this kind of thought. It can be said that no harm, callback makes the system more flexible.
 When a function pointer is a function parameter, passed to a called function, the called function can invoke the external function through this pointer, which forms the callback function in the general program is not very obvious, can not use this form the most important purpose is when the function is not in the same file, For example, a dynamic library, to invoke functions in other programs only in the form of callbacks include "Stdio.h" #include "conio.h" int Add (int a, int b), int libfun (int (*pdis) (int a, int b) int main (void) {int (*pfun) (int a, int b);p fun = Add;libfun (pfun);//Call int libfun (int (*pdis) (int a, int b))}int Add (int a , int b) {return a + B;} int libfun (int (*pdis) (int a, int b))//callback Add () function {int A, B;a = 1;b = 2;printf ("%d", Pdis (A, b));} Now that these functions are in the same file, if int libfun (int (*pdis) (int a, int b) is a function in a library, only the callback is used, and the Code of the external function address passed through the function pointer parameter is modified to implement the calling function Add. You do not have to change the code of the Library, you can implement the call to facilitate the maintenance and upgrade of the program 
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.