首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将void (__thiscall MyClass:: * )(void *)转换为void (__cdecl * ) (void *)指针

如何将void (__thiscall MyClass:: * )(void *)转换为void (__cdecl * ) (void *)指针
EN

Stack Overflow用户
提问于 2011-03-16 21:47:43
回答 1查看 8.5K关注 0票数 4

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

代码语言:javascript
复制
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);
}

我找遍了也找不到。有没有人做过这样的事?还是我走错路了?提亚

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-03-16 21:51:03

你不能这么做。

您应该使用静态函数(不是静态成员函数,而是自由函数)。

代码语言:javascript
复制
// 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));
}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5326251

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档