Casting between void * and a pointer to member function

前端 未结 5 1881
故里飘歌
故里飘歌 2020-11-27 07:25

I\'m currently using GCC 4.4, and I\'m having quite the headache casting between void* and a pointer to member function. I\'m trying to write an easy-to-use li

5条回答
  •  遥遥无期
    2020-11-27 08:12

    Unlike the address of a nonstatic member function, which is a pointer-to-member type with a complicated representation, the address of a static member function is usually a just a machine address, compatible with a conversion to void *.

    If you need to bind a C++ non-static member function to a C or C-like callback mechanism based on void *, what you can try to do is write a static wrapper instead.

    The wrapper can take a pointer to an instance as an argument, and pass control to the nonstatic member function:

    void myclass::static_fun(myclass *instance, int arg)
    {
       instance->nonstatic_fun(arg);
    }
    

提交回复
热议问题