静态函数和非静态变量
今天用定时器的时候出现下面的问题:
class CVideoSurveillance
{
private:
static void __stdcall CAMERAALERT_TimerProc(void* lp);
int CurrentID;
}
void __stdcall CVideoSurveillance::CAMERAALERT_TimerProc(void* lp)
{
CVideoSurveillance *t = (CVideoSurveillance *)lp;
WF_MSG* MSG;
CMessagePool::GetInstance()->RequestMsg(&MSG);
MSG->nMessageCode = WFMSG_VS_ALERT_GETSTATUS; //向V3(5)发送获取移动侦测消息
MSG->lParam1 = t->CurrentID; //MSG->lParam1 = CurrentID编译通不过
MSG->lParam2 = 0;
t->PostMsg(MSG);
}
开始我写的是MSG->lParam1 = CurrentID
error C2597: illegal reference to non-static member 'CVideoSurveillance::CurrentID'
error C3867: 'CVideoSurveillance::CurrentID': function call missing argument list; use '&CVideoSurveillance::CurrentID' to create a pointer to member
因为CAMERAALERT_TimerProc是静态函数(静态函数是不能直接调用类的非静态变量),而由于类可以不实例化,则可调用静态函数,即可以不用CVideoSurveillance t; t->CAMERAALERT_TimerProc();而直接CVideoSurveillance::CAMERAALERT_TimerProc();调用静态函数。而对于类里的非静态成员函数或变量必须实例化后,才能调用,所以才出现刚刚的编译通不过。
浙公网安备 33010602011771号