Several methods for C + + static member functions to access non-static members

Source: Internet
Author: User
Tags call back

We all know that the member functions of the classes in C + + provide the this pointer by default, and in non-static member functions, when you call a function, the compiler will "automatically" help you add the this pointer to the function parameter. Of course, under C + + flexibility, classes also have static members and static functions, i.e.

class a{public:    staticvoid  Test ()    {        1 ;    } Private :     Static int M_statica;     int m_a};

At this point your test function can only access the M_statica members, not the m_a. Students may ask, what is the problem? The problem is reflected in the application scenario, and I did not mind it at first, until I met the annoying problem of the callback function and I really thought about this knowledge point.

First of all, the callback, we should all know the meaning of the callback, the callback in C is the main embodiment of the callback function, of course, C + + also has other usages such as functor, throw away these, simply at the point of the callback function we discuss the following.

Because the member functions of the C + + class All imply this pointer, if I register directly, for example

void (A::*= a::hello;p ();

At this point the program will error, the message is that you are missing a this pointer, means that you want to really want to use p, you must have a space to allocate an instance to call

void (A::*=new  A ();(a. *P) ();(p a->*p) ();

Of course, if you are simply registering a callback function on a C + + class static function, you do not need to consider this pointer

void (A::*= a::test;p ();

But the problem is that you can't have your member variables at the moment, you see, the problem comes . In the face of this demand, we should really calm down to think about how to let the static function to access the non-static member variable problem.

Method One:

A very trickery approach is to add the address of the instance to the formal parameter table of the static function, which is

class a{public:    staticvoid Test (A *a)    {        a1 ;    }     void Hello ()    {    }private:    staticint  m_statica;     int m_a};

This allows you to access non-static member variables by using a static function that does not have access to the member's non-static variables (too clumsy) when you call back the function.

Method Two:

In fact, this method is used in a lot of glib, is put on the global variable address is

A g_a; class a{public:    staticvoid  Test ()    {        1 ;    }     void Hello ()    {    }private:    staticint  m_statica;     int m_a};

This approach we understand is good, global variables we do not recommend.

Method Three:

Everyone knows that static member functions cannot access non-static members, but don't forget that they can access static members, that is, if our class is a singleton , we can assign the this pointer to that static member at the time of creation, Then inside the static member function can be assured that the use of bold.

classa{ Public: A () {M_ga= This; }    Static voidTest () {m_ga.m_a+=1; }    voidhello () {}Private:    Static intM_statica; StaticAM_ga; intm_a};

Method Four:

Compared to the method, but his direction is more about the concept of memory block, meaning that the parameter ratio of a static function plus a void * memory header address, and then internal conversion

 class   a{ public  :  static  void  Test (void  *    pdata)        {a  *a = (a *) PData;    A ->m_a + = 1   void   Hello () {}  private  :  static  int   M_statica;  int   m_a}; A a;test ( &a); 

As above, I have organized 4 methods, of course, there are many, in fact, the way around so much, our hope is not to break the callback function neat function interface (plus its own instance pointer) and make a compromise, if you prefer to change the interface or by using Java similar interface way to achieve, There is no problem, here is mainly to provide you with a train of thought, C + + is really flexible, we have to use this double-edged sword:)

Several methods for C + + static member functions to access non-static members

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.