问个多态的基本问题
void Manager::Run()
{
while( WaitForSingleObject(m_hKillEvent, 0) != WAIT_OBJECT_0 ){
PTRMSG pMsg = m_msgQueue.WaitForNormalMessage(1000);
if( pMsg ){
OnMessage(pMsg);
DeleteMessage(pMsg);
}
}
}
------
这里函数里OnMessage是个虚函数,在Manager.h中的定义是:
virtual BOOL OnMessage(PTRMSG pMsg) = NULL;
但实际的实现是Manager类的子类实现的,但有好几个子类实现,从上面的直接调用OnMessage(pMsg)虚函数,那么到底是指向哪个子类怎么看的?(隐藏指针?
从WaitForNormalMessage函数返回的给PTRMSG pMsg 指针的赋值体现的?)
附:
PTRMSG CMessageQueue::WaitForNormalMessage(int dwMilliseconds)
{
DWORD dw = WaitForSingleObject(m_hEvent, dwMilliseconds);
if( dw == WAIT_OBJECT_0 )
return PopupMessage();
return NULL;
}
PTRMSG CMessageQueue::PopupMessage()
{
PTRMSG pRet = NULL;
EnterCriticalSection(&m_lock);
POSITION pos = m_listMsg.GetHeadPosition();
if( pos ){
pRet = (PTRMSG)m_listMsg.GetAt(pos);
m_listMsg.RemoveAt(pos);
if( m_listMsg.GetCount() )
SetEvent(m_hEvent);
// else ResetEvent(m_hEvent); // 自动事件不需要设置
}
LeaveCriticalSection(&m_lock);
ASSERT(pRet);
return pRet;
}