Usage analysis of CallBack function of CallBack in C++

  • 2020-04-02 02:58:29
  • OfStack

This article illustrates the use of CallBack functions in C++. Share with you for your reference. Specific analysis is as follows:

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).     Instead of using a member function, to access a member variable of a class, you can use the friend operator, which is simply described in C++ as a friend of the class.

2).     Use the static member function, which 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.
Example:

#include "stdafx.h"  
#include <iostream> 
#include <assert.h> 
using namespace std; 
 
class Test 

public: 
    friend void callBackFun(void){ cout << "CallBack Function!";} //Because callBackFun has a const Test* pointer & NBSP; by default; < br / > }; 
 
typedef void (*FPtr)(void); 
void Fun(FPtr ptr) 

    ptr(); 

int main(void) 

    Fun(callBackFun);  
 
    return 0; 
}

Hope that the article described in the C++ programming to help you.


Related articles: