Cast pointer to member function to normal pointer

前端 未结 6 628
抹茶落季
抹茶落季 2020-11-28 15:52

Currently I have a class of this kind, shortened for simplicity:

class MyClass {
    public: 
        MyClass();
        void* someFunc(void* param);
}
         


        
6条回答
  •  北荒
    北荒 (楼主)
    2020-11-28 16:35

    The difference between a C function and a C++ member function is that C function uses cdecl calling convention, while member functions uses thiscall calling convention (and you can't even take their address!).

    As I understand, you actually want that secondFunc() to call the member function of a particular instance of class (let's call it this). Well, addresses of member functions of all the instances of a particular class are the same. In order to pass the pointer to the object, you will need a side channel. In this case it could be static variable. Or, if you want MT support, you'll have to use Thread Local Storage (TLS),

    This requires one callback per SomeFunc-type member, but you would need a dispatcher somewhere anyway.

提交回复
热议问题