分享

MFC关键技术模拟(2)

 戴维图书馆 2014-05-11

          接上一篇:MFC关键技术模拟(1)

        http://blog.csdn.net/bzhou830/article/details/25505291

       上次仿真了MFC的类层次结构,这次开始仿真MFC程序的初始化过程。首先给出类的继承和包含的函数图表。这种图表方式可能相比资源框里面的那种方式更加容易理解。


按照这个图表,在上一次的源码上添加成员函数。

mfc.h

  1. #include <iostream>  
  2.   
  3. using namespace std;  
  4.   
  5. class CObject  
  6. {  
  7. public:  
  8.     CObject::CObject()  
  9.     {  
  10.         cout<<"CObject Constructor"<<endl;  
  11.     }  
  12.     CObject::~CObject()  
  13.     {  
  14.         cout<<"CObject Destructor"<<endl;  
  15.     }  
  16. };  
  17.   
  18.   
  19. class CCmdTarget : CObject  
  20. {  
  21. public:  
  22.     CCmdTarget::CCmdTarget()  
  23.     {  
  24.         cout<<"CCmdTarget Constructor"<<endl;   
  25.     }  
  26.   
  27.     CCmdTarget::~CCmdTarget()  
  28.     {  
  29.         cout<<"CCmdTarget Destructor"<<endl;   
  30.     }  
  31. };  
  32.   
  33. class CWinThread : public CCmdTarget  
  34. {  
  35. public:  
  36.     CWinThread::CWinThread()  
  37.     {  
  38.         cout<<"CWinThread Constructor"<<endl;   
  39.     }  
  40.     CWinThread::~CWinThread()  
  41.     {  
  42.         cout<<"CWinThread Destructor"<<endl;   
  43.     }  
  44.     virtual void CWinThread::InitInstance()  
  45.     {  
  46.         cout<<"CWinThread::InitInstance"<<endl;  
  47.     }  
  48.     virtual int CWinThread::Run()  
  49.     {  
  50.         cout<<"CWinThread::Run"<<endl;  
  51.         return 1;  
  52.     }  
  53.   
  54. };  
  55.   
  56. class CWnd;    //因为CwinApp类中添加了CWnd*类型,故在此需要做说明                           
  57.   
  58. class CWinApp : public CWinThread  
  59. {  
  60. public:  
  61.     CWinApp * m_pCurrentWinApp;  
  62.     CWnd* m_pMainWnd;  
  63. public:  
  64.     CWinApp::CWinApp()  
  65.     {  
  66.         m_pCurrentWinApp = this;  
  67.         cout<<"CWinApp Constructor"<<endl;   
  68.     }  
  69.     CWinApp::~CWinApp()  
  70.     {  
  71.         cout<<"CWinApp Destructor"<<endl;   
  72.     }  
  73.     virtual void CWinApp::InitApplication()  
  74.     {  
  75.         cout<<"CWinApp::InitApplication"<<endl;  
  76.     }  
  77.     virtual void CWinApp::InitInstance()  
  78.     {  
  79.         cout<<"CWinApp::InitInstance"<<endl;  
  80.     }  
  81.     virtual int CWinApp::Run()  
  82.     {  
  83.         cout<<"CWinApp::Run"<<endl;  
  84.         return CWinThread::Run();  
  85.     }  
  86. };  
  87.   
  88.   
  89. class CDocument:public CCmdTarget  
  90. {  
  91. public:  
  92.     CDocument::CDocument()  
  93.     {  
  94.         cout<<"CDocument Constructor"<<endl;  
  95.     }  
  96.     CDocument::~CDocument()  
  97.     {  
  98.         cout<<"CDocument Destructor"<<endl;  
  99.     }  
  100. };  
  101.   
  102. class CWnd:public CCmdTarget  
  103. {  
  104. public:  
  105.     CWnd::CWnd()  
  106.     {  
  107.         cout<<"CWnd Constructor"<<endl;  
  108.     }  
  109.     CWnd::~CWnd()  
  110.     {  
  111.         cout<<"CWnd Destructor"<<endl;  
  112.     }  
  113.     virtual void CWnd::Creat()  
  114.     {  
  115.         cout<<"CWnd::Creat"<<endl;  
  116.     }  
  117.     virtual void CWnd::PreCreatWindow()  
  118.     {  
  119.         cout<<"CWnd::PreCreatWindow"<<endl;  
  120.     }  
  121.     int CWnd::CreateEx()  
  122.     {  
  123.         cout<<"CWnd::CreateEx"<<endl;  
  124.         PreCreatWindow();  
  125.         return 1;  
  126.     }  
  127. };  
  128.   
  129. class CFrameWnd:public CWnd  
  130. {  
  131. public:  
  132.     CFrameWnd::CFrameWnd()  
  133.     {  
  134.         cout<<"CFrameWnd Constructor"<<endl;  
  135.     }  
  136.     CFrameWnd::~CFrameWnd()  
  137.     {  
  138.         cout<<"CFrameWnd Destructor"<<endl;  
  139.     }  
  140.     void CFrameWnd::Creat()  
  141.     {  
  142.         cout<<"CFrameWnd::Creat"<<endl;  
  143.         CreateEx();  
  144.     }  
  145.     virtual void CFrameWnd::PreCreatWindow()  
  146.     {  
  147.         cout<<"CFrameWnd::PreCreatWindow"<<endl;  
  148.     }  
  149.   
  150. };  
  151.   
  152. class CView : public CWnd  
  153. {  
  154. public:  
  155.     CView::CView()  
  156.     {  
  157.         cout<<"CView Constructor"<<endl;  
  158.     }  
  159.     CView::~CView()  
  160.     {  
  161.         cout<<"CView Destructor"<<endl;  
  162.     }  
  163.   
  164. };  
  165.   
  166.   
  167. CWinApp * AfxGetApp();  
mfc.c
  1. #include "my.h"  
  2.   
  3. extern CMyWinApp theApp;  
  4.   
  5.   
  6. CWinApp * AfxGetApp()  
  7. {  
  8.     return theApp.m_pCurrentWinApp;  
  9. }  

my.h
  1. #include <iostream>  
  2. #include "mfc.h"  
  3.   
  4. class CMyFrameWnd :public CFrameWnd  
  5. {  
  6. public:  
  7.     CMyFrameWnd::CMyFrameWnd()  
  8.     {  
  9.         Creat();  
  10.         cout<<"CMyFrameWnd Constructor"<<endl;  
  11.     }  
  12.     CMyFrameWnd::~CMyFrameWnd()  
  13.     {  
  14.         cout<<"CMyFrameWnd Destructor"<<endl;  
  15.     }  
  16. };  
  17.   
  18. class CMyWinApp:public CWinApp  
  19. {  
  20. public:  
  21.     CMyWinApp::CMyWinApp()  
  22.     {  
  23.        cout<<"CMyWinApp Constructor"<<endl;  
  24.     }  
  25.     CMyWinApp::~CMyWinApp()  
  26.     {  
  27.         cout<<"CMyWinApp Destructor"<<endl;  
  28.     }  
  29.     virtual void CMyWinApp::InitInstance()  
  30.     {  
  31.         cout<<"CMyWinApp::InitInstance"<<endl;  
  32.         m_pMainWnd = new CMyFrameWnd;  
  33.     }  
  34. };  
my.c
  1. #include "my.h"  
  2.   
  3. CMyWinApp theApp;  
  4.   
  5. void main()  
  6. {  
  7.     CWinApp* pApp = AfxGetApp();  
  8.     pApp->InitApplication();  
  9.     pApp->InitInstance();  
  10.     pApp->Run();  
  11.     system("pause");  
  12. }  
运行的结果:

这个运行结果和书上一致。下面来分析过程。

1、在程序进入main函数之前,构建了全局对象theApp,引发构造函数的执行。

2、进入main函数,CWinApp* pApp = AfxGetApp(); 这样pApp就指向了theApp全局对象。

3、 pApp->InitApplication();执行的是CWinApp::InitApplication();

4、pApp->InitInstance();执行的是CMyWinApp::InitInstance();  因为CWinApp的子类CMyWinApp对虚函数InitInsyance进行了改写。

     ----CMyWinApp::InitInstance(); 中定义了 m_pMainWnd = new CMyFrameWnd;这样引发了CMyFrameWnd的构造函数的运行,当然之前会运行其父类的构造函数。

     ----然而在CMyFrameWnd的构造函数中调用Create()函数,由于Create()是虚函数,并且在CMyFrameWnd类中也没有对其进行改写,所以引发CFrameWnd类的Create函数。

    ---- CFrameWnd的Create函数又调用CreateEx()函数。他仅是一个父类成员函数,不是虚函数。所以就执行父类CWnd类的CreateEx();

    ----CWnd类的CreateEx();中执行PreCreateWindow()函数,这又是一个虚函数,并且子类对其做了改写,那么就执行子类CFrameWnd的PreCreateWindow()函数。

5、pApp->Run();执行的是CWinApp::Run();

至此,整个过程就完成了。里面涉及的主要是虚函数,确实体现出了虚函数特点和用途。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多