#include <iostream>
using namespace std;
class CWinApp
{
public:
CWinApp *m_pCurrentWinApp;
public:
CWinApp() {
m_pCurrentWinApp = this;
cout <<"CWinApp Constructor"<<endl;
}
virtual void InitApplication() {
cout <<"CwinApp::InitApplication"<<endl;}
virtual void InitInstance() {
cout <<"CWinApp::InitInstance"<<endl;}
~CWinApp() {
cout <<"CWinApp Destructor"<<endl;}
};
class CMyWinApp : public CWinApp
{
public:
CMyWinApp() {
cout <<"CMyWinApp Constructor"<<endl;}
~CMyWinApp() {
cout <<"CMyWinApp Destructor"<<endl;}
virtual void InitInstance () {
cout <<"CMyWinApp::InitInstance"<<endl;}
};
CMyWinApp theApp;
CWinApp *AfxGetApp()
{
return theApp.m_pCurrentWinApp;
}
int _tmain(int argc, _TCHAR* argv[])
{
CWinApp *pApp=AfxGetApp();
pApp->InitInstance ();
return 0;
}
以下是执行结果:
CWinApp Constructor
CMyWinApp Constructor
CMyWinApp::InitInstance //问题在这不解.
CMyWinApp Destructor
CWinApp Destructor
Press any key to continue
即然函数AfxGetApp()返回的是基类型指针,怎么还会输出CMyWinApp::InitInstace.这条信息呢?
正常理解应该输出CWinApp::InitInstance.