如何将void(__thiscall MyClass :: *)(void *)转换为void(__cdecl *)(void *)指针 [英] How to convert void (__thiscall MyClass::* )(void *) to void (__cdecl *)(void *) pointer

查看:1243
本文介绍了如何将void(__thiscall MyClass :: *)(void *)转换为void(__cdecl *)(void *)指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想构建一个IThread类,它可以隐藏线程创建。子类实现了ThreadMain方法并使其自动调用,如下所示:

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:

class IThread
{
public:
    void BeginThread();
    virtual void ThreadMain(void *) PURE;
};
void IThread::BeginThread()
{
    //Error : cannot convert"std::binder1st<_Fn2>" to "void (__cdecl *)(void *)"
    m_ThreadHandle = _beginthread(
                     std::bind1st( std::mem_fun(&IThread::ThreadMain), this ),
                     m_StackSize, NULL);
    //Error : cannot convert void (__thiscall* )(void *) to void (__cdecl *)(void *)
      m_ThreadHandle = _beginthread(&IThread::ThreadMain, m_StackSize, NULL);
}

我已经搜索过了,无法弄清楚。有没有人做这样的事情?还是我走错了路? TIA

I have searched around and cannot figure it out. Is there anybody who did such thing? Or I am going the wrong way? TIA

推荐答案

您不能。

静态函数(不是静态成员函数,而是一个自由函数)。

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));
}

这篇关于如何将void(__thiscall MyClass :: *)(void *)转换为void(__cdecl *)(void *)指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆