The order in which MFC objects are created

Source: Internet
Author: User

All objects in MFC are rooted in CObject and CObject derive some classes: CCmdTarget, CWnd, CDocument. The specific hierarchical relationships are as follows:


Figure (1) The hierarchical relationship of MFC

This case is based on the MFC program code, using the WIN32 Console application program (ie console application) to emulate MFC's internal behavior, of course, omitting the message map and message loop.

Create a new console project with the name Frame1 and the detailed code as follows:

Mfc.h

#include <iostream.h>class cobject{public:cobject::cobject () {cout<< "CObject Constructor \ n";} Cobject::~cobject () {cout<< "CObject destructor \ n";}; Class Ccmdtarget:public Cobject{public:ccmdtarget::ccmdtarget () {cout<< "CCmdTarget Constructor \ n";} Ccmdtarget::~ccmdtarget () {cout<< "CCmdTarget destructor \ n";}; Class Cwinthread:public Ccmdtarget{public:cwinthread::cwinthread () {cout<< "CWinThread Constructor \ n";} Cwinthread::~cwinthread () {cout<< "CWinThread destructor \ n";}; Class Cwinapp:public cwinthread{public:cwinapp* M_pcurrentwinapp;public:cwinapp::cwinapp () {m_pCurrentWinApp=this; cout<< "CWinApp Constructor \ n";} Cwinapp::~cwinapp () {cout<< "CWinApp destructor \ n";}; Class Cdocument:public ccmdtarget{public:cdocument::cdocument () {cout<< "CDocument Constructor \ n";} Cdocument::~cdocument () {cout<< "CDocument destructor \ n";}; Class Cwnd:public Ccmdtarget{public:cwnd::cwnd () {cout<< "CWnd Constructor \ n";} Cwnd::~cwnd () {COUT&LT;&Lt; " CWnd destructor \ n ";}}; Class Cframewnd:public Cwnd{public:cframewnd::cframewnd () {cout<< "CFrameWnd Constructor \ n";} Cframewnd::~cframewnd () {cout<< "CFrameWnd destructor \ n";}; Class Cview:public Cwnd{public:cview::cview () {cout<< "CView Constructor \ n";} Cview::~cview () {cout<< "CView destructor \ n";}};/ /global functioncwinapp* AfxGetApp ();


Mfc.cpp

#include "my.h" extern  CMyWinApp Theapp; cwinapp* AfxGetApp () {return theapp.m_pcurrentwinapp;}

My.h

#include <iostream.h> #include "mfc.h" class Cmywinapp:public Cwinapp{public:cmywinapp::cmywinapp () {cout< < "CMyWinApp Constructor \ n";} Cmywinapp::~cmywinapp () {cout<< "CMyWinApp destructor \ n";};

My.cpp

#include "my.h" CMyWinApp Theapp; Global Objectvoid Main () {cwinapp* Papp = AfxGetApp ();}

The results are as follows:

Analytical:

So, you see, Frame1 does not have any new object, but there is a global object Theapp exist, C + + rule: The construction of the global object will be more than the program entry point (in the DOS environment is main, in the Windows environment is WinMain) earlier, so Theapp 's constructor will be older than the main () function. That is, the output operation of the above constructor (XXX Constructor) is done before the main () function.

The main () function calls the global function AfxGetApp () to get the Theapp object pointer, which is exactly how the MFC program is emulated.

Reference: Hou Junjie. MFC (Second Edition). Wuhan Huazhong University Press, 2001

The order in which MFC objects are created

Related Article

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.