c++ 从静态函数调用非静态函数 [英] c++ calling a non static function from a static function
问题描述
当我尝试调用非静态函数时遇到访问冲突错误.
我的 .h
文件看起来像这样.
类世界{民众:世界();虚拟〜世界();静态无效回调调度回调(SIMCONNECT_RECV *pData,DWORD cbData,无效 *pContext);无效进程(SIMCONNECT_RECV *pData,DWORD cbData);虚空 frameEvent();虚空初始化();};
现在在我的 .cpp 文件中的 init() 函数中,我调用了一个将数据返回给我的回调函数的函数.
SimConnect_CallDispatch(hSimConnect, DispatchCallback, NULL);
将数据发送到 DisPatchCallback
函数.
在这个函数中存在以下代码:
void CALLBACK World::DispatchCallback(SIMCONNECT_RECV *pData,DWORD cbData,无效 *pContext){World *pThis = reinterpret_cast(pContext);pThis->Process(pData, cbData);}
这个函数是一个静态函数,它创建一个 World 对象来调用 Process 函数.
这有效,但它在想要访问 frameEvent 函数的行中断.
void World::Process(SIMCONNECT_RECV *pData, DWORD cbData){HRESULT 小时;开关(pData-> dwID){案例 SIMCONNECT_RECV_ID_EVENT_FRAME:框架事件();//它在这里中断 frameEvent 是一个非静态函数休息;}}
<块引用>
访问违规读取位置 0x00000000.
有人能指出我解决这个问题的正确方向吗?
如果您想知道我正在为 Microsoft Flight Simulator X 编写插件.
我正在尝试以 oo 的方式实现 simconnect.h
.Msdn 显示了我正在尝试实施的示例.
class CName{无效调度();静态无效调度回调(SIMCONNECT_RECV *pData,DWORD cbData,无效 *pContext);无效进程(SIMCONNECT_RECV *pData,DWORD cbData);处理 hSimConnect;//使用 SimConnect_Open 设置此值.};void CName::Dispatch(){::SimConnect_Dispatch(hSimConnect, &CName;::DispatchCallback, this);}//静态函数void CName::DispatchCallback(SIMCONNECT_RECV *pData,DWORD cbData,无效 *pContext){CName *pThis = reinterpret_cast(pContext);pThis->Process(pData, cbData);}void CName::Process(SIMCONNECT_RECV *pData, DWORD cbData){//处理 SimConnect 数据}
我希望我提供了足够的信息.
您将 NULL
作为上下文参数传递给 SimConnect_CallDispatch
,因此您的回调不知道哪个调用 Process
的 World
对象——如果你不告诉它怎么可能?更改调用以将 this
作为上下文参数传入,就像示例一样:
SimConnect_CallDispatch(hSimConnect, DispatchCallback, this);
I'm experiencing an access violation error when I try to call non static function.
my .h
file looks like this.
class World
{
public:
World();
virtual ~World();
static void CALLBACK DispatchCallback(
SIMCONNECT_RECV *pData,
DWORD cbData,
void *pContext
);
void Process(SIMCONNECT_RECV *pData, DWORD cbData);
virtual void frameEvent();
virtual void init();
};
Now in my .cpp file the init() function I call a function that return data to my callback function.
SimConnect_CallDispatch(hSimConnect, DispatchCallback, NULL);
Which sends data to the DisPatchCallback
function.
In this function the following code resides:
void CALLBACK World::DispatchCallback(
SIMCONNECT_RECV *pData,
DWORD cbData,
void *pContext)
{
World *pThis = reinterpret_cast<World*>(pContext);
pThis->Process(pData, cbData);
}
this function is a static function which creates a World object to call the Process function.
This works but it break on the line where it wants to access the frameEvent function.
void World::Process(SIMCONNECT_RECV *pData, DWORD cbData)
{
HRESULT hr;
switch(pData->dwID)
{
case SIMCONNECT_RECV_ID_EVENT_FRAME:
frameEvent(); //it breaks here frameEvent is a non static function
break;
}
}
Access violation reading location 0x00000000.
Can someone point me to the right direction as to solve this issue.?
If you wonder I'm writing a plugin for Microsoft Flight Simulator X.
I'm trying to implement simconnect.h
in a oo way. Msdn shows an example that I'm trying to implement.
class CName
{
void Dispatch();
static void DispatchCallback(
SIMCONNECT_RECV *pData,
DWORD cbData,
void *pContext
);
void Process(SIMCONNECT_RECV *pData, DWORD cbData);
HANDLE hSimConnect; // use SimConnect_Open to set this value.
};
void CName::Dispatch()
{
::SimConnect_Dispatch(hSimConnect, &CName;::DispatchCallback, this);
}
// static function
void CName::DispatchCallback(
SIMCONNECT_RECV *pData,
DWORD cbData,
void *pContext)
{
CName *pThis = reinterpret_cast<CName*>(pContext);
pThis->Process(pData, cbData);
}
void CName::Process(SIMCONNECT_RECV *pData, DWORD cbData)
{
// do processing of SimConnect data
}
I hope I gave enough information.
You're passing NULL
as the context parameter to SimConnect_CallDispatch
, so your callback has no idea which World
object to call Process
on -- how could it possibly if you don't tell it? Change the call to pass in this
as the context parameter, like the example does:
SimConnect_CallDispatch(hSimConnect, DispatchCallback, this);
这篇关于c++ 从静态函数调用非静态函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!