How to implement a member function of a class as a callback function

  • 2020-04-02 01:46:42
  • OfStack

If you try to use C++ member functions directly as callbacks, an error will occur and even compilation will fail. The error is that ordinary C++ member functions all imply a transfer function as an argument, i.e., the "this" pointer. C++ realizes that the program function can access C++ data members by passing this pointer to its member function. This also explains why multiple instances of a C++ class can share member functions - with different data members. Due to the function of this pointer, when a call-back member function is installed as a callback function, the number of parameters of the function will not match because of the implicit this pointer, which leads to the failure of the callback function installation. The key to solving this problem is to keep this pointer out of the picture, and the problem of using callback functions in C++ can be solved using two typical techniques. This method is universal and suitable for any C++.

1). Do not use the member function. In order to access the member variable of the class, you can use the friend operator.

2). Use the static member function, the static member function does not use this pointer as an implicit parameter, so it can be used as a callback function. Static member functions have two characteristics: one, can be used without class instances; Second, only static member variables and static member functions can be accessed, and non-static member variables and non-static member functions cannot be accessed. Since the purpose of using class member functions as callbacks in C++ is to access all member variables and functions, failure to do so would not make sense. The solution is simply to use a static class pointer as a class member, initialize the static pointer at class creation time, such as pThis=this, and then access all the member variables and functions in the callback function through the static pointer. This works for cases where there is only one class instance, because multiple class instances will share static class members and static member functions, which results in a static pointer to the last class instance created. To avoid this, you can use a parameter of the callback function to pass this pointer, thereby enabling data member sharing. This method is a little cumbersome and will not be repeated here.

First understand what is a callback function: for example, the called function void callbackf (int n) {} in order to be a callback function, callbackf must appear as a parameter of the main callback function, such as void f (void (*p (int)), int n) form!

Example:

Case 1:


#include<iostream>
using namespace std;
class A
{
public:

 friend void callback()  //The friend function is implemented as a callback function friend
 {
  cout<<" The callback function is executing! "<<endl;
 }
};
void f(void (*p)())
 {
  p();
 }
int main()
{
 void (*p)();
 p=callback;
 f(p);
 return 0;
}

Example 2:

#include<iostream>
using namespace std;
class A
{
public:

 static void callback()  //A member function of a class is implemented as a callback function static
 {
  cout<<" The callback function is executing! "<<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 the class:

#include<iostream>
using namespace std;
class A
{
public:

 static void callback()  //A member function of a class is implemented as a callback function static
 {
  cout<<" The callback function is executing! "<<endl;
 }
 void f(void (*p)())
 {
  p();
 }
};
int main()
{
 A a;
 void (*p)();
 p=A::callback;
 a.f(p);
 return 0;
}


Related articles: