Use class member functions as C callback Functions

Source: Internet
Author: User
Tags call back
Use class member functions as C callback Functions

Raise questions:
Callback functions are based on the C Programming windows SDK technology, not for C ++. programmers can directly use a C function as a callback function, however, if you try to directly use the member function of C ++ as the callback function, an error will occur, and even compilation will fail.
Cause analysis:
C ++ member functions of Putong imply a passing function as a parameter, that is, the "This" pointer, c ++ transfers a pointer to itself to its member functions so that program functions can access data members of C ++. This can also understand why multiple instances of the C ++ class can share member functions but have different data members. Because of the function of this pointer, the installation of a callback-type membership function as a callback function fails because the implicit this pointer does not match the number of function parameters.
Solution:
1. Do not use member functions. You can use the friend operator (friend) to directly use common C functions to enable member variables of member classes in C functions ), in C ++, you can describe the C function as a friend of the class. This processing mechanism is the same as using callback functions in common C programming.
2. Using static member functions, static member functions do not use the this pointer as an implicit parameter, so that they can be used as Callback functions. Static member functions have two major features: first, they can be used without class instances; second, they can only access static member variables and static member functions, you cannot access non-static member variables or non-static member functions. In C ++, the purpose of using a class member function as a callback function is to access all member variables and member functions. If this function is not implemented, it will not be of practical significance. We solve the problem by using static member functions to wrap non-static member functions. Class instances can be passed to static member functions by attaching parameters or global variables. Examples:
1. parameter transmission method
# 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 ....
};

// Static packaging function, which can call the member function display (). It is used as a callback function.
Void tclassa: wrapper_to_call_display (void * pt2object, char * string)
{
// Explicit type conversion
Tclassa * myself = (tclassa *) pt2object;

// Call common member functions
Myself-> display (string );
}

// The host of the callback function. The callback function is used here.
Void doita (void * pt2object, void (* pt2function) (void * pt2object, char * Text ))
{
// Use the callback function
Pt2function (pt2object, "Hi, I'm calling back using a argument ;-)");
}

// Execution example
Void callback_using_argument ()
{
Tclassa obja;
Doita (void *) & obja, tclassa: wrapper_to_call_display );
}

2. Global Variables
# Include <iostream. h>
Void * pt2object; // global variable, which 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 packaging function
Void tclassb: wrapper_to_call_display (char * string)
{
// Ensure that the global variable value is correct.
Tclassb * myself = (tclassb *) pt2object;
Myself-> display (string );
}

// Call back the host of the function. Here, the call back function is used.
Void doitb (void (* pt2function) (char * Text ))
{

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

// Execution example
Void callback_using_global ()
{
Tclassb objb;
Pt2object = (void *) & objb;
Doitb (tclassb: wrapper_to_call_display );
}

Note: The comparison between the above two methods shows that the static packaging function in the 2nd methods can maintain the same signature as the common member function. When the host interface of the callback function cannot be changed, this method is particularly useful. However, the use of global variables 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.