How to call a non static member function from a static member function without passing class instance

后端 未结 4 592
温柔的废话
温柔的废话 2020-12-16 06:36

I need to call a non static member function from a static member function of the same class. The static function is a callback. It can receive only void as data, though whic

4条回答
  •  一生所求
    2020-12-16 07:13

    This is the only way :

    #include 
    #include 
    
    struct A;
    A *oneObj = NULL;
    
    
    struct A
    {
      A(){
        oneObj=this;
      }
      ~A(){
        oneObj=NULL;
      }
      void foo()
      {
      }
    
      static void boo()
      {
        assert( NULL != oneObj );
        oneObj->foo();
      }
    };
    
    int main()
    {
      A onlyOne;
      A::boo();
    }
    

提交回复
热议问题