[英]c++ calling a non static function from a static function
I'm experiencing an access violation error when I try to call non static function. 我尝试调用非静态函数时遇到访问冲突错误。
my .h
file looks like this. 我的
.h
文件看起来像这样。
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. 现在在我的.cpp文件中的init()函数我调用一个函数将数据返回给我的回调函数。
SimConnect_CallDispatch(hSimConnect, DispatchCallback, NULL);
Which sends data to the DisPatchCallback
function. DisPatchCallback
数据发送到DisPatchCallback
函数。
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. 此函数是一个静态函数,它创建一个World对象来调用Process函数。
This works but it break on the line where it wants to access the frameEvent function. 这可以工作,但它打破了它想要访问frameEvent函数的行。
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.
访问冲突读取位置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. 如果你想知道我正在为Microsoft Flight Simulator X编写一个插件。
I'm trying to implement simconnect.h
in a oo way. 我正试图以oo方式实现
simconnect.h
。 Msdn shows an example that I'm trying to implement. Msdn显示了我正在尝试实现的示例。
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? 您将
NULL
作为上下文参数传递给SimConnect_CallDispatch
,因此您的回调不知道哪个World
对象要调用Process
on - 如果您不告诉它,怎么可能呢? Change the call to pass in this
as the context parameter, like the example does: 更改调用以传入
this
作为上下文参数,如示例所示:
SimConnect_CallDispatch(hSimConnect, DispatchCallback, this);
It breaks because this
is NULL. 它因为
this
是NULL而中断。
Check this portion of your code: 检查代码的这一部分:
World *pThis = reinterpret_cast<World*>(pContext);
pThis->Process(pData, cbData);
If the reinterpret_cast
fails
gets a NULL - it returns NULL, and you don't check for it. 如果
reinterpret_cast
失败
获得NULL - 它返回NULL,并且您不检查它。
Your use of: 您的使用:
SimConnect_CallDispatch(hSimConnect, DispatchCallback, NULL);
seems very suspicious. 似乎非常可疑。 What's the
NULL
parameter mean in the call to that function? 在该函数的调用中,
NULL
参数的含义是什么? Is that pContext
? 是
pContext
吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.