MFC框架里
union MessageMapFunctions
{
AFX_PMSG pfn; void (CWnd::*pfn_vv)(void);
void (CWnd::*pfn_vw)(UINT);
int (CWnd::*pfn_is)(LPTSTR);
};
处理消息时
const AFX_MSGMAP* pMessageMap;
const AFX_MSGMAP_ENTRY* lpEntry;
for(pMessageMap = GetMessageMap(); pMessageMap != NULL; pMessageMap = pMessageMap->pBaseMap)
{
ASSERT(pMessageMap != pMessageMap->pBaseMap);
if((lpEntry = AfxFindMessageEntry(pMessageMap->pEntries, message, 0, 0)) != NULL)
goto LDispatch;
}
return FALSE;LDispatch:
union MessageMapFunctions mmf;
mmf.pfn = lpEntry->pfn;
switch(lpEntry->nSig)
{
default:
return FALSE;
case AfxSig_vw:
(this->*mmf.pfn_vw)(wParam);
// ((*mmf.pfn_vw))(wParam);这样不行
break;
为什么我去掉this->后不行了呢?编译错误error C2171: '*' : illegal on operands of type 'void (__thiscall CWnd::*)(unsigned int)'
: error C2064: term does not evaluate to a function
哪位仁兄请指点下小弟
union MessageMapFunctions
{
AFX_PMSG pfn; void (CWnd::*pfn_vv)(void);
void (CWnd::*pfn_vw)(UINT);
int (CWnd::*pfn_is)(LPTSTR);
};
处理消息时
const AFX_MSGMAP* pMessageMap;
const AFX_MSGMAP_ENTRY* lpEntry;
for(pMessageMap = GetMessageMap(); pMessageMap != NULL; pMessageMap = pMessageMap->pBaseMap)
{
ASSERT(pMessageMap != pMessageMap->pBaseMap);
if((lpEntry = AfxFindMessageEntry(pMessageMap->pEntries, message, 0, 0)) != NULL)
goto LDispatch;
}
return FALSE;LDispatch:
union MessageMapFunctions mmf;
mmf.pfn = lpEntry->pfn;
switch(lpEntry->nSig)
{
default:
return FALSE;
case AfxSig_vw:
(this->*mmf.pfn_vw)(wParam);
// ((*mmf.pfn_vw))(wParam);这样不行
break;
为什么我去掉this->后不行了呢?编译错误error C2171: '*' : illegal on operands of type 'void (__thiscall CWnd::*)(unsigned int)'
: error C2064: term does not evaluate to a function
哪位仁兄请指点下小弟
调用类成员函数指针的运算符是->* 和 .**mmf.pfn_vw在编译器看来是错误的语法
((*mmf.pfn_vw))(wParam)不要认为编译器会在这里会给你默认添加this->
// ((*mmf.pfn_vw))(wParam);这样不行
成员函数调用需要this指针,所以需要指向对象的指针或者通过对象来访问
成员函数指针也是这个道理。