Treatment of C ++ callback functions

zhaozj2021-02-12  135

Since the DDEML mechanism requires a callback function, the key to use DDEML is to solve the use of callback functions in the MFC programming system. The callback function (Callback function) is a large number of system services for Windows. By it, the programmer can install the device driver and message filtering system to control the effective use of Windows. Many programmers have found that writing callback functions using MFC or other C applications is very troublesome, and the root cause is that the callback function is based on C-programmed Windows SDK technology. It is not for C , and the programmer can direct a C function. As a callback function, if you try to use the C member function as a callback function, it will not pass it even compile. By querying the information, its error is that the ordinary C member function is implied with a transfer function as a parameter, that is, "this" pointer, C to achieve the program function to access C by passing a pointer to its own pointer Data members. This can also be understood why multiple instances of the C class can share member functions but have different data members. Due to the role of the THIS pointer, it will make the function parameters matches the number of the function parameters because the hidden THIS pointer is installed, resulting in the number of callback functions to fail. The key to solving this problem is to do not let the THIS pointer work, by using the following two typical techniques, can solve the problems encountered in C to use the callback function. This method has versatility and is suitable for any C .

1). Do not use the member function, directly use the normal C function, in order to implement the member variables of the class in the C function, you can use the friend operator (Friend), and the C function will be described as the class friend in C . I.e. This processing mechanism is the same as the callback function in normal C programming.

2). Use a static member function, static member functions do not use the THIS pointer as an implied parameter, so that it can be used as a callback function. The static member function has two features: one, can be used without class; second, only access to static member variables and static member functions, and cannot access non-static member variables and non-static member functions. Since the purpose of using class member functions in C is the purpose of the callback function to access all member variables and member functions, if this will not be practical. The solution is also very simple, just use a static class pointer as a class member, initialize the static pointer when creating, such as pTHIS = this, then pass the static pointer in the callback function to access all member variables and member functions . This method of processing is applicable to only one class instance, because multiple class instances share static classes and static member functions, which causes static pointers to point to the final created class instance. To avoid this, you can use a parameter of the callback function to deliver the THIS pointer to implement data member sharing. This method is slightly troublesome, here is not described again.

转载请注明原文地址:https://www.9cbs.com/read-7669.html

New Post(0)