如何将地址转换为函数指针到调用方法

mah*_*esh 1 c c++ multithreading function-pointers

我想在WaitAndCallFunc()函数中调用Test1()方法.

码:

typedef void (*func)();

void StartTimer(void* pFuncAddr);
void WaitAndCallFunc(void* pPtr);

void WaitAndCallFunc(void* pPtr)
{
    int i = 0;
    int nWaitTime = 3;

    while(1)
    {
        Sleep(1000);
    //  I want pPtr to call Test1 Function;
        if(i == nWaitTime)
            break;
    }

    _endthread();
}
void StartTimer(void* pFuncAddr)
{
    _beginthread(WaitAndCallFunc, 0, pFuncAddr);
}
void Test1();
int main(int argc, char* argv[])
{

    StartTimer(Test1);

    Sleep(5000);

    return 0;
}

void Test1()
{
    cout << "Testing Thread\n";
}
Run Code Online (Sandbox Code Playgroud)

Gre*_*ill 5

我不确定我明白你的问题是什么,但试试这个:

((func)pPtr)();
Run Code Online (Sandbox Code Playgroud)