How to make a class member function as a callback function

Source: Internet
Author: User
Many programmers have found that using MFC or other C ++ applications to write callback functions is very troublesome. The root cause is that callback functions are based on C-programming windows SDK technology, not for C ++, programmers can directly use a C function as a callback function. However, if they try to directly use a C ++ member function as a callback function, an error will occur, even compilation fails. Through data query, it is found that the error is that common C ++ member functions 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, when a callback-type member function is used as a callback function, the implicit this pointer causes the number of function parameters to be mismatched, resulting in a callback function installation failure. The key to solving this problem is not to let this pointer work. The following two typical technologies can be used to solve the problems encountered by using callback functions in C ++. This method is universal and suitable for any c ++.

1 ). without using a member function, you can directly use a common C function. To implement member variables of the member class in a C function, you can use the friend operator (friend ), 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). Use a static member function. The static member function does not use the this pointer as an implicit parameter, so that it can be used as a callback function. 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. The solution is also very simple, that is, to use a static class pointer as a class member, by initializing this static pointer during class creation, such as pthis = This, then, the static pointer can be used in the callback function to access all member variables and member functions. This method applies to the case where there is only one class instance, because multiple class instances share static class members and static member functions, which leads to the static pointer pointing to the last class instance. To avoid this situation, you can use a callback parameter to pass the this pointer, so as to share data members. For this method, see the following example:

Class ctestclass
{
PRIVATE:
Typedef int (ctestclass: * funcptr) (INT, INT );
Int _ A, _ B;
Int getplusresult (int A, int B)
{
Return A + B;
}
Int getmultiplyresult (int A, int B)
{
Return a * B;
}
Protected:
Int F (int A, int B, funcptr PTR)
{
Return (this-> * PTR) (A, B );
}
Public:
Int getaplusb ()
{
Return F (_ A, _ B, getplusresult );
}
Int getamultiplyb ()
{
Return F (_ A, _ B, getmultiplyresult );
}
Ctestclass (int A, int B): _ B (B), _ A (){};
~ Ctestclass (void );
};

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.