Use a class member function as a C callback function __ function

Source: Internet
Author: User
Tags call back wrapper
To use a class member function as a C callback function

Ask a question:
Callback functions are based on the C programming Windows SDK Technology, not for C + +, programmers can be a C function directly as a callback function, but if you try to directly use C + + member functions as a callback function will have errors, or even compile can not pass.
Analysis Reason:
Ordinary C + + member functions imply a pass-through function as an argument, that is, the "This" pointer, which is implemented by passing a pointer to itself to its member function, thereby enabling the program function to access the data members of C + +. This also makes sense of why multiple instances of a C + + class can share member functions but do have different data members. Because the this pointer is used to install a member function of type callback as a callback function, a callback function installation fails because the implied this pointer causes the number of function parameters to not match
Solution:
First, do not use member functions, direct use of ordinary C functions, in order to achieve in the C function can access the class member variable, you can make the Ufida operator (friend), in C + + in the C function as a friend of the class can be. This processing mechanism is the same as using callback functions in normal C programming.
Second, using static member functions, static member functions do not use the this pointer as implied parameters, so that they can be used as callback functions. Static member functions have two main features: first, they can be used without class instances; second, static member variables and static member functions are accessible, and non-static member variables and non-static member functions are inaccessible. Because using a class member function as a callback function in C + + is intended to access all member variables and member functions, it would not be practical to do so. We solve the problem by using static member functions to wrap the non-static member functions. Class instances can be passed to static member functions in the form of additional parameters or global variables. Examples are as follows:
1, the way the parameter is passed
#include <iostream.h>
Class Tclassa
{
Public

void Display (const char* text) {cout << text << Endl;};
static void Wrapper_to_call_display (void* pt2object, char* text);
More ....
};

A static wrapper function that can invoke the member function display () itself as a callback function to use
void Tclassa::wrapper_to_call_display (void* pt2object, char* string)
{
Explicit type conversions
tclassa* myself = (tclassa*) pt2object;

Calling normal member functions
Myself->display (string);
}

The host of the callback function, where the call back function is used
void Doita (void* pt2object, Void (*pt2function) (void* pt2object, char* text)
{
Using Callback functions
Pt2function (Pt2object, "Hi, I ' m calling back using a argument;-)");
}

Execution sample
void Callback_using_argument ()
{
Tclassa Obja;
Doita ((void*) &obja, Tclassa::wrapper_to_call_display);
}

2, the way of global variables
#include <iostream.h>
void* Pt2object; Global variable, can point to any object
Class TCLASSB
{
Public

void Display (const char* text) {cout << text << Endl;};
static void Wrapper_to_call_display (char* text);

};

Static wrapper function
void Tclassb::wrapper_to_call_display (char* string)
{
Need to ensure the correctness of global variable values
tclassb* myself = (tclassb*) pt2object;
Myself->display (string);
}

Back to the host of the calling function, where the call function is used
void Doitb (void (*pt2function) (char* text)
{

Pt2function ("Hi, I ' m calling back using a global;-)"); Make callback
}

Execution sample
void Callback_using_global ()
{
TCLASSB OBJB;
Pt2object = (void*) &objB;
DOITB (Tclassb::wrapper_to_call_display);
}

Note: By comparing the two methods above, the static wrapper function in the 2nd method can be consistent with the normal member function, which is particularly useful when the host interface of the callback function cannot be changed. But because of the use of global variables, it is not a good design.

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.