To pass a pointer to a member function

后端 未结 4 780
闹比i
闹比i 2021-01-13 03:51

I have an class with instance functions (or methods?). From within an instance, I try to pass pointers to those functions to a library. The library expects static functions

4条回答
  •  走了就别回头了
    2021-01-13 04:45

    It is possible and apparently safe to use a file scoped static variable pointing to your GlutInstance (static function + static data, like mentioned in another answer).

    static GlutController* s_this;
    
    static void s_OnChangeSize(int w, int h) { s_this->OnChangeSize(w, h); }
    
    GlutController::GlutController (int argc, char **argv) { 
       s_this = this;
    
       glutSpecialFunc(s_OnSpecialKeys);
    }
    
    GlutController::~GlutController() { s_this= 0; } 
    
    void GlutController::OnChangeSize(int w, int h) { /* non-static stuff */ }
    

    s_this is only visible in the local file, e.g. not visible to any code that invokes the GlutController constructor from another file.

提交回复
热议问题