How to convert void (__thiscall MyClass::* )(void *) to void (__cdecl *)(void *) pointer

后端 未结 1 1476
渐次进展
渐次进展 2021-01-13 17:47

I want to build a \"IThread\" class which can hide the thread creation. Subclass implements the \"ThreadMain\" method and make it called automatically which seems like this:

相关标签:
1条回答
  • 2021-01-13 17:58

    You can't.

    You should use a static function instead (not a static member function, but a free function).

    // IThread.h
    class IThread
    {
    public:
        void BeginThread();
        virtual void ThreadMain() = 0;
    };
    
    // IThread.cpp
    extern "C"
    {
        static void __cdecl IThreadBeginThreadHelper(void* userdata)
        {
            IThread* ithread = reinterpret_cast< IThread* >(userdata);
            ithread->ThreadMain();
        }
    }
    void IThread::BeginThread()
    {
        m_ThreadHandle = _beginthread(
                         &IThreadBeginThreadHelper,
                         m_StackSize, reinterpret_cast< void* >(this));
    }
    
    0 讨论(0)
提交回复
热议问题