C++ class member function callback

前端 未结 3 730
鱼传尺愫
鱼传尺愫 2020-12-03 13:07

I have the following problem. I have a function from an external library (which cannot be modified) like this:

void externalFunction(int n, void udf(double*)         


        
3条回答
  •  情书的邮戳
    2020-12-03 13:44

    Here is how I do this, when MyClass is a singleton:

    void externalFunction(int n, void udf(double) );
    
    class MyClass
    {
    public:
       static MyClass* m_this;
       MyClass(){ m_this = this; }
       static void mycallback(double* x){ m_this->myrealcallback(x); }
       void myrealcallback(double* x);
    }
    
    int main()
    {
       MyClass myClass;
       externalFunction(0, MyClass::mycallback);
    }
    

提交回复
热议问题