A non-static member variable or a non-static member function in a calling class in a callback function

Source: Internet
Author: User

In this regard, let me begin by saying something about:

The callback function must be a static member function or a global function to implement a callback function, presumably because a normal C + + member function implies a function parameter, the this pointer, C + + By passing the this pointer to a member function, the implementation function can access the data members of a particular object of the class. Because of the reason of this pointer, a normal member function as a callback function causes the function parameter number to not match because of the implied this pointer problem, which results in the failure of the callback function compilation.

Based on the above theory, how do you encapsulate a callback function in a class?

A callback function can only be a global function or a static member function, but because the global function destroys encapsulation, only static member functions can be used as callback functions to encapsulate the callback function in the class.

But if there is such a requirement, when an event is triggered, I want to access the class's ordinary member functions and ordinary member variables, what should I do? First, it is clear that the normal member function cannot be used as a callback function, so the normal member function cannot be directly recalled after the event is triggered. Then there can only be one way, when the event is triggered, the callback class's static member function, and then try to access the ordinary member functions and ordinary member variables through the static member functions of the class.

Note: Because there is no this pointer to the static member function of the class, you cannot access the normal member variables of the class directly, nor can you access the ordinary member functions of the class. It's all about finding a way.

1. How to enable static functions to access non-static members of a class

Declares a static function A (), passing the class instance object pointer as a parameter

Class A () {static void A (a *pthis);//static function void B ();   Non-static function};  void A::a (A *pthis) {pthis->b ();//static function call non-static function}

  

2. Accessing non-static members in the callback function (the callback function here can only be a static member function of the Class)

Because the callback function often has a fixed definition, does not accept A * PThis parameters, such as: CALLBACK Mytimerproc (HWND hwnd,uint umsg,uint idevent,dword dwtime);

Solution one does not use the object pointer as a parameter to a static member function, but rather as a static member variable of the class. This allows direct access to the object pointer in the static member function (which is already used as a callback function), using the object pointer to manipulate ordinary member variables and ordinary member functions.

Class A () {  static void A ();//static callback function  void B ();  Non-static function static   A * pThis;   static object pointer};  A * A::p this=null; The a::a ()   ///constructor assigns the this pointer to PThis, allowing the callback function to access the object {Pthis=this through pThis pointers   ;} void A::a () {  if (pthis==null)   return;  Pthis->b (); Call non-static function in callback function}

  

http://blog.csdn.net/bzhxuexi/article/details/19831667

A non-static member variable or a non-static member function in the calling class in a callback function

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.