My Opinion on the MFC framework-Simulation Analysis

Source: Internet
Author: User

My Opinion on the MFC framework-Simulation Analysis

Review the knowledge of MFC, and review Hou Jie's in-depth introduction. below is the code used to simulate the running of the MFC framework.<Br/> // MFC. h <br/> // written by leezhm 27st August, 2008 <br/> // contact: leezhm@126.com <br/> // </P> <p> // ------------ cobject class <br/> // | --- c0000target class <br/> // | <br/> // | --- cwinthread class <br/> // | --- cwinapp class <br/> // | <br/> // | --- cmyapp class <br/> // | --- cwnd <br/> // | <br/>/ /| --- cview class <br/> // | <br/> // | --- Cmyview class <br/> // | --- cframewnd class <br/> // | <br/>/| | --- cmyframewnd class <br/> // | --- cdocument class <br/> // | --- cmydoc class <br /># ifndef _ h_mfc __< br/> # DEFINE _ h_mfc __< br/> class cobject <br/>{< br/> public: <br/> cobject (); <br/> ~ Cobject (); <br/>}; <br/> class c0000target: Public cobject <br/>{< br/> Public: <br/> c0000target (); <br/> ~ C0000target (); <br/>}; <br/> class cwinthread: Public c0000target <br/>{< br/> Public: <br/> cwinthread (); <br/> ~ Cwinthread (); <br/> Public: <br/> virtual bool initinstance (); <br/> virtual bool run (); <br/> }; <br/> class cwnd; <br/> class cwinapp: Public cwinthread <br/>{< br/> Public: <br/> cwinapp (); <br/> ~ Cwinapp (); <br/> Public: <br/> virtual bool initapplication (); <br/> virtual bool initinstance (); <br/> virtual bool run (); <br/> Public: <br/> cwinapp * m_pcurrentwinapp; <br/> cwnd * m_pmainwnd; <br/>}; <br/> class cwnd: public c0000target <br/>{< br/> Public: <br/> cwnd (); <br/> ~ Cwnd (); <br/> Public: <br/> virtual bool create (); <br/> bool createex (); <br/> virtual bool precreatewindow (); <br/>}; <br/> class cview: Public cwnd <br/>{< br/> Public: <br/> cview (); <br/> ~ Cview (); <br/>}; <br/> class cframewnd: Public cwnd <br/>{< br/> Public: <br/> cframewnd (); <br/> ~ Cframewnd (); <br/> Public: <br/> bool create (); <br/> virtual bool precreatewindow (); <br/> }; <br/> class cdocument: Public c0000target <br/>{< br/> Public: <br/> cdocument (); <br/> ~ Cdocument (); <br/>}; <br/> # endif/_ h_mfc __< br/> // <br/> // MFC. CPP <br/> // <br/> # include "MFC. H "<br/> # include <iostream> <br/> Using STD: cout; <br/> Using STD: Endl; <br/> cobject :: cobject () <br/>{< br/> cout <"cobject constructor" <Endl; <br/>}< br/> cobject ::~ Cobject () <br/>{< br/> cout <"cobject destructor" <Endl; <br/>}< br/> c0000target: c0000target () <br/>{< br/> cout <"c0000target constructor" <Endl; <br/>}< br/> c0000target ::~ C0000target () <br/>{< br/> cout <"c0000target destructor" <Endl; <br/>}< br/> cwinthread: cwinthread () <br/>{< br/> cout <"cwinthread constructor" <Endl; <br/>}< br/> cwinthread ::~ Cwinthread () <br/>{< br/> cout <"cwinthread destructor" <Endl; <br/>}< br/> bool cwinthread: initinstance () <br/>{ <br/> cout <"cwinthread: initinstance" <Endl; <br/> return true; <br/>}< br/> bool cwinthread: Run () <br/>{< br/> cout <"cwinthread: Run" <Endl; <br/> return true; <br/>}< br/> cwinapp: cwinapp () <br/>{< br/> m_pcurrentwinapp = this; <br/> cout <"cwinapp constructor" <Endl; <br/>}< br/> CW Inapp ::~ Cwinapp () <br/>{< br/> cout <"cwinapp destructor" <Endl; <br/>}< br/> bool cwinapp: initapplication () <br/>{< br/> cout <"cwinapp: initapplication" <Endl; <br/> return true; <br/>}< br/> bool cwinapp: initinstance () <br/>{< br/> cout <"cwinapp: initinstance" <Endl; <br/> return true; <br/>}< br/> bool cwinapp: Run () <br/>{< br/> cout <"cwinapp :: run "<Endl; <br/> return cwinthread: Run (); <br/>}< br/> C WND: cwnd () <br/>{< br/> cout <"cwnd constructor" <Endl; <br/>}< br/> cwnd ::~ Cwnd () <br/>{< br/> cout <"cwnd destructor" <Endl; <br/>}< br/> bool cwnd: Create () <br/>{< br/> cout <"cwnd: Create" <Endl; <br/> return true; <br/>}< br/> bool cwnd: createex () <br/>{< br/> cout <"cwnd: createex" <Endl; </P> <p> precreatewindow (); <br/> return true; <br/>}< br/> bool cwnd: precreatewindow () <br/>{< br/> cout <"cwnd: precreatewindow" <Endl; <br/> return true; <br/>}< br/> cview:: cview () <Br/>{< br/> cout <"cview constructor" <Endl; <br/>}< br/> cview ::~ Cview () <br/>{< br/> cout <"cview destructor" <Endl; <br/>}< br/> cframewnd: cframewnd () <br/>{< br/> cout <"cframewnd constructor" <Endl; <br/>}< br/> cframewnd ::~ Cframewnd () <br/>{< br/> cout <"cframewnd destructor" <Endl; <br/>}< br/> bool cframewnd: Create () <br/>{< br/> cout <"cframewnd: Create" <Endl; <br/> createex (); <br/> return true; <br/>}< br/> bool cframewnd: precreatewindow () <br/>{< br/> cout <"cframewnd: precreatewindow" <Endl; <br/> return true; <br/>}< br/> cdocument: cdocument () <br/>{< br/> cout <"cdocument constructor" <Endl; <br/>}< br/> CDO Cument ::~ Cdocument () <br/>{< br/> cout <"cdocument destructor" <Endl; <br/>}< br/> // <br/> // mymfc. h <br/> // <br/> # ifndef _ h_mymfc __< br/> # DEFINE _ h_mymfc __< br/> # include "MFC. H "<br/> // global function <br/> cwinapp * mygetwinapp (); <br/> class cmyapp: Public cwinapp <br/>{< br/> public: <br/> cmyapp (); <br/> ~ Cmyapp (); <br/> virtual bool initinstance (); <br/>}; <br/> class cmyframewnd: Public cframewnd <br/>{< br/> public: <br/> cmyframewnd (); <br/> ~ Cmyframewnd (); <br/>}; <br/> # endif/_ h_mymfc __< br/> // <br/> // mymfc. CPP <br/> // <br/> # include "mymfc. H "<br/> # include <iostream> <br/> Using STD: cout; <br/> Using STD: Endl; <br/> // global variable <br/> cmyapp theapp; <br/> cwinapp * mygetwinapp () <br/> {<br/> If (! Theapp. m_pcurrentwinapp) <br/> return NULL; <br/> return theapp. m_pcurrentwinapp; <br/>}< br/> cmyapp: cmyapp () <br/>{< br/> cout <"cmyapp constructor" <Endl; <br/>}< br/> cmyapp ::~ Cmyapp () <br/>{< br/> cout <"cmyapp destructor" <Endl; <br/>}< br/> bool cmyapp: initinstance () <br/>{< br/> cout <"cmyapp: initinstance" <Endl; <br/> m_pmainwnd = new cmyframewnd (); <br/> return true; <br/>}< br/> cmyframewnd: cmyframewnd () <br/>{< br/> cout <"cmyframewnd constructor" <Endl; <br/> Create (); <br/>}< br/> cmyframewnd ::~ Cmyframewnd () <br/>{< br/> cout <"cmyframewnd destructor" <Endl; <br/>}< br/> void main () <br/>{< br/> cout <Endl; <br/> cwinapp * PAPP = mygetwinapp (); <br/> PAPP-> initapplication (); <br/> PAPP-> initinstance (); <br/> PAPP-> Run (); <br/> cout <Endl; <br/>}


OK. After the code is pasted, check whether the running result is correct.

In this image, we should pay attention to the calling rules of constructors and destructor During the creation and destruction of global cmyapp objects, as well as the call sequence of each function, we need to understand the inheritance and polymorphism principles of the class.

In this image, we can see the inheritance relationship of each class. We also need to note that the address of each function in the virtual function table in the subclass is consistent with the class polymorphism.

All right, the running results and debugging results are on the top. According to the above analysis:

First, during the creation of the global variable theapp, the constructor calls of sub-classes and parent classes comply with the c ++ syntax. First, the top parent class constructor is used, then, the constructor of the final subclass is followed by the constructor; The Destructor is exactly the opposite.

Then you enter the main function. First, you will get the this pointer and the global variable theapp.

PAPP-> initapplication (...) because the cmyapp class does not overwrite the virtual functions in the base class cwinapp (override, also called overwrite, which is distinguished from the overload of class members ), according to the principle of inheritance and polymorphism, cwinapp: initapplication (...) should be called (...) function

PAPP-> initinstance (...) because the virtual function initinstance of the base class is inherited and override in cmyapp, cmyapp: initinstance (...) is called (...) function. Note that in this function, the statement m_pmainwnd = new cmyframewnd (); creates a cmyframewnd object, which must trigger a call to construct a sequence base class.

Note that the create member function is called in the constructor of cmyframewnd, And the create function calls the createex inherited function from the base class, while the createex function calls the precreatewindow function. At this time, let's take a closer look at the source code and know that cframewnd inherits from the base class cwnd and overwrites the precreatwindow function. Therefore, the cframewnd: precreatewindow (...) function is called at this time. Understanding this number is a key point.

Finally, the call of the run function is still inherited and the principle of polymorphism. Pay attention to the implementation of cwinapp: Run (...).

Bool cwinapp: Run () <br/>{< br/> cout <"cwinapp: Run" <Endl; <br/> return cwinthread :: run (); <br/>}< br/>

It returns cwinthread: Run (...), which is why this part does not conform to the principle of inherited rewriting.


 

Now, the entire analysis is complete. In the next article, we will analyze the entire simulation process with the source code of MFC.

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.