gpt4 book ai didi

c++ - 如何将 "this"指针传递给全局函数

转载 作者:行者123 更新时间:2023-11-28 07:05:22 25 4
gpt4 key购买 nike

//This is AsynchronousFunction class header file

typedef int (*functionCall)(int, int);

DWORD __stdcall functionExecuter(LPVOID pContext); // global function

class AsynchronousFunction
{

int param1, param2;
functionCall fCall;
HANDLE m_handle;

public:
AsynchronousFunction(functionCall, int, int);
~AsynchronousFunction();
int result();

protected:
private:
int returnVal;

};


It's implementation as follows

AsynchronousFunction::AsynchronousFunction(functionCall fCall, int param1, int param2):m_handle(CreateEvent( NULL , false , false , NULL))
{
bool b = QueueUserWorkItem(functionExecuter, this, WT_EXECUTEDEFAULT);

WaitForSingleObject(m_handle, INFINITE);
SetEvent(m_handle);
}

AsynchronousFunction::~AsynchronousFunction()
{
CloseHandle(m_handle);
}

int AsynchronousFunction::result()
{

return 0;// not implemented yet

}

DWORD __stdcall functionExecuter(LPVOID pContext)
{

return 0;

}

此处 pContext 接收“this”指针。我的尝试是访问 param1 和 param2
从这里开始工作我该怎么做?

最佳答案

或者你可以让 functionExecuter 成为 AsynchronousFunction 的友元或者在 AsynchronousFunction 中添加一个公共(public)函数,它执行所需的操作并从 functionExecuter 中调用它,如下所示。

DWORD __stdcall functionExecuter(LPVOID pContext)
{
return (reinterpret_cast<AsyncrhonousFunction*>(pContext))->realFunctionExecuter();
}

关于c++ - 如何将 "this"指针传递给全局函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21844440/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com