Convert C++ function pointer to c function pointer

后端 未结 7 1083
无人共我
无人共我 2020-12-01 04:42

I am developing a C++ application using a C library. I have to send a pointer to function to the C library.

This is my class:

 class MainWindow : pub         


        
相关标签:
7条回答
  • 2020-12-01 05:26

    You can't pass a non-static member function pointer as an ordinary function pointer. They're not the same thing, and probably not even the same size.

    You can however (usually) pass a pointer to a static member function through C. Usually when registering a callback in a C API, you also get to pass a "user data" pointer which gets passed back to your registered function. So you can do something like:

    class MyClass
    {
    
        void non_static_func(/* args */);
    
    public:
        static void static_func(MyClass *ptr, /* other args */) {
            ptr->non_static_func(/* other args */);
        }
    };
    

    Then register your callback as

    c_library_function(MyClass::static_func, this);
    

    i.e. pass the instance pointer to the static method, and use that as a forwarding function.

    Strictly speaking for total portability you need to use a free function declared extern "C" rather than a static member to do your forwarding (declared as a friend if necessary), but practically speaking I've never had any problems using this method to interface C++ code with GObject code, which is C callback-heavy.

    0 讨论(0)
提交回复
热议问题