这个程序我又改了一下.
#include <iostream>
#include <typeinfo>
using namespace std;
class CWinApp
{
public:
CWinApp *m_pCurrentWinApp;
public:
CWinApp() { m_pCurrentWinApp = this;
cout <<"CWinApp Constructor"<<endl;}
~CWinApp() {cout <<"CWinApp Destructor"<<endl;}
virtual bool InitInstance() {cout <<"CWinApp::InitInstance"<<endl;return true;}
};
class CMyWinApp : public CWinApp
{
public:
CMyWinApp() {cout <<"CMyWinApp Constructor"<<endl;}
~CMyWinApp() {cout <<"CMyWinApp Destructor"<<endl;}
virtual bool InitInstance() {cout <<"CMyWinApp::InitInstance"<<endl;return true;}
};
int _tmain(int argc, _TCHAR* argv[])
{
CMyWinApp theApp;
cout <<typeid(*theApp.m_pCurrentWinApp).name()<<endl;
return 0;
}
cout <<typeid(*theApp.m_pCurrentWinApp).name()
这条语句输出的是class CMyWinApp 类型.
theApp.m_pCurrentWinApp 是一个CWinApp类型指针
在什么时候theApp.m_pCurrentWinApp指针,指向了CMyWinApp类型呢?