How to make a member function of a class as a callback function

Source: Internet
Author: User
Tags static class
If you try to use C + + 's member function directly as a callback function, you will get an error, even if the compilation fails. Its error is that the normal C + + member function implies a transfer function as a parameter, that is, the "This" pointer, C + + by passing the this pointer to its member function so that the program function can access C + + data members. This also allows you to understand why multiple instances of a C + + class can share member functions but-there are different data members. Because of the role of the this pointer, a CALL-BACK member function is installed as a callback function because the implied this pointer does not match the number of function arguments, which causes the callback function to fail to install. The key to solving this problem is to not let the this pointer work, by using the following two typical techniques to solve the problems encountered in using callback functions in C + +. This method is versatile and suitable for any C + +.

1). Do not use member functions, in order to access member variables of the class, you can make the user-friendly meta-operator (friend), in C + +, the function is described as a friend of the class.

2). Using a static member function, the static member function does not use the this pointer as an implicit argument, so it can be used as a callback function. Static member functions have two major characteristics: first, they can be used without class instances, and only static member variables and static member functions are accessible, and non-static member variables and non-static member functions cannot be accessed. Because the purpose of using a class member function as a callback function in C + + is to access all member variables and member functions, it does not make sense to do so. The solution is also simple to use a static class pointer as a class member, by initializing the static pointer when the class is created, such as Pthis=this, and then accessing all member variables and member functions through the static pointer in the callback function. This approach applies to cases where there is only one class instance, because multiple class instances share static class members and static member functions, which results in a static pointer pointing to the last class instance 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 is a bit of a hassle, so don't repeat it here. (See http://www.cnblogs.com/this-543273659/archive/2011/08/29/2157966.html for static methods to access non-static variables and functions)

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

Example:

Example 1:

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

friend Void Callback ()//friend function as callback function friend way implementation
{
cout<< "callback function has started executing. "<<endl;
}
};
void f (void (*p) ())
{
P ();

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

Example 2:

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

static void callback ()//member function of class is implemented as callback function static method
{
cout<< "callback function has started executing. "<<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:

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

static void callback ()//member function of class is implemented as callback function static method
{
cout<< "callback function has started executing. "<<endl;
}
void f (void (*p) ())
{
P ();

}
};

int main ()
{
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.