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

Source: Internet
Author: User
Tags static class

If you try to use a member function of C + + directly as a callback function, an error occurs, and even compilation cannot pass. The error is that ordinary C + + member functions implicitly have a pass-through function as an argument, that is, the "This" pointer, which C + + passes through the this pointer to its member functions to enable program functions to access data members of C + +. This also makes sense of why multiple instances of a C + + class can share member functions-with different data members. Because the this pointer is used to install a member function of type Call-back as a callback function, a callback function installation fails because the implied this pointer causes the number of function parameters to not match. The key to solving this problem is to not allow this pointer to work, and you can solve the problem of using callback functions in C + + by using the following two typical techniques.  This method is versatile and suitable for any C + +.

1. Do not use the member function, in order to access the class member variables, you can make the Ufida operator (friend), in C + + to describe the function as a friend of the class can be.

2. Using a static member function, a static member function does not use the this pointer as a suppressed parameter, so that it can be used as a callback function. 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. The solution is also simple: to use a static class pointer as a class member, you can access all member variables and member functions by initializing the static pointer when the class is created, such as Pthis=this, and then through the static pointer in the callback function. This approach applies to cases where there is only one instance of a class, because more than one class instance will share static class members and static member functions, causing the static pointer to point to the class instance that was last created. To avoid this situation, you can use a parameter of the callback function to pass the this pointer, which enables data member sharing. This method is a little troublesome, and there is no more to repeat.

First you understand what a callback function is: for example, the called function void Callbackf (int n) {} to be a callback function, CALLBACKF must appear as a parameter of the calling function, such as void f (void (*p (int)), int n).

Example:

Example 1:

Copy Code code as follows:

#include <iostream>
using namespace Std;
Class A
{
Public

friend Void Callback ()//friend function as a callback function friend method implementation
{
cout<< "The callback function begins to execute!" "<<endl;
}
};
void f (void (*p) ())
{
P ();

}
int main ()
{
void (*p) ();
P=callback;
f (P);
return 0;
}


Example 2:
Copy Code code as follows:

#include <iostream>
using namespace Std;
Class A
{
Public

The member function of the static void callback ()//class is implemented as a static method of the callback function
{
cout<< "The callback function begins to execute!" "<<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 a class:
Copy Code code as follows:

#include <iostream>
using namespace Std;
Class A
{
Public

The member function of the static void callback ()//class is implemented as a static method of the callback function
{
cout<< "The callback function begins to execute!" "<<endl;
}
void f (void (*p) ())
{
P ();

}
};

int main ()
{
A A;
void (*p) ();
P=a::callback;
A.F (P);
return 0;
}

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.