How to use a class member function as a callback function

Source: Internet
Author: User

If you try to directly use the member function of C ++ as the callback function, an error will occur, and even compilation will fail. The error is that a common C ++ member function implies a passing function as a parameter, that is, the "This" pointer, c ++ passes the this pointer 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 different data members exist. This pointer makes it possible to install a call-back member function as a callback function because the implicit this pointer does not match the number of function parameters, this causes the callback function to fail to be installed. 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). Do not use member functions. For member variables of the member class, you can use the friend operator (friend) to describe this function as a friend of the class in C ++.

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. Since the purpose of using class member functions as Callback functions in C ++ is to access all member variables and member functions, it is meaningless to do so. 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. This method is a little troublesome and I will not go into details here. (For the way static methods access non-static variables and functions, see the http://www.cnblogs.com/this-543273659/archive/2011/08/29/2157966.html)

First understand what is a callback function: for example, if the called void callbackf (int n) {} function is to be called as a callback function, callbackf must be used as the form parameter of the main function, for example, void F (void (* P (INT), int N!

 

Example:

Example 1:

# Include <iostream>
Using namespace STD;
Class
{
Public:
 
Friend void callback () // The friend function is implemented as the callback function in the friend mode.
{
Cout <"the callback function is being executed! "<Endl;
}
};
Void F (void (* p )())
{
P ();

}
Int main ()
{
Void (* p )();
P = callback;
F (p );
Return 0;
}

Example 2:

# Include <iostream>
Using namespace STD;
Class
{
Public:
 
Static void callback () // class member functions are implemented as Callback functions in static mode
{
Cout <"the callback function is being executed! "<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 the class:

# Include <iostream>
Using namespace STD;
Class
{
Public:
 
Static void callback () // class member functions are implemented as Callback functions in static mode
{
Cout <"the callback function is being executed! "<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.