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<≪ " 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