看《深入浅出MFC》,讲到AfxGetApp获取的是全局对象theApp,具体代码在://AfxWin1.inl
_AFXWIN_INLINE CWinApp* AFXAPI AfxGetApp()
{ return afxCurrentWinApp; }//Afxwin.h
#define afxCurrentWinApp AfxGetModuleState()->m_pCurrentWinApp
...
也就是说AfxGetApp()相当于:AfxGetModuleState()->m_pCurrentWinApp
而theApp的构造函数里只有这样一个保存自己指针的地方:CWinApp::CWinApp(LPCTSTR lpszAppName)
{
...
pModuleState->m_pCurrentWinApp = this;
...
}不知道AfxGetModuleState()->m_pCurrentWinApp到底是如何得到这个theApp的this指针的?
_AFXWIN_INLINE CWinApp* AFXAPI AfxGetApp()
{ return afxCurrentWinApp; }//Afxwin.h
#define afxCurrentWinApp AfxGetModuleState()->m_pCurrentWinApp
...
也就是说AfxGetApp()相当于:AfxGetModuleState()->m_pCurrentWinApp
而theApp的构造函数里只有这样一个保存自己指针的地方:CWinApp::CWinApp(LPCTSTR lpszAppName)
{
...
pModuleState->m_pCurrentWinApp = this;
...
}不知道AfxGetModuleState()->m_pCurrentWinApp到底是如何得到这个theApp的this指针的?
对了,你右键变量pModuleState不就可以找到它的定义说明了吗~~~
pModuleState->m_pCurrentWinApp = this;
可以简化为:
AfxGetModuleState()->m_pCurrentWinApp = this;而AfxGetApp()的返回值为AfxGetModuleState()->m_pCurrentWinApp,这样就得到了theApp的this指针。现在唯一不明白的就是AfxGetModuleState到底做了什么?因为没有找到它的源代码