1.MFC类层次结构
2.模拟程序
头文件MFC.H
#include <iostream>
using namespace std;
class CObject
{
public:
CObject::CObject(){ cout<<"CObject Constructor"<<endl;}
CObject::~CObject(){cout<<"CObject Destructor"<<endl;}
};
class CCmdTarget:public CObject
{
public:
CCmdTarget::CCmdTarget(){cout<<"CCmdTarget Constructor"<<endl;}
CCmdTarget::~CCmdTarget(){cout<<"CCmdTarget Destructor"<<endl;}
};
class CWinThread:public CCmdTarget
{
public:
CWinThread::CWinThread(){cout<<"CWinThread Constructor"<<endl;}
CWinThread::~CWinThread(){cout<<"CWinThread Destructor"<<endl;}
};
class CWinApp:public CWinThread
{
public:
CWinApp* m_pCurrentWinApp;
public:
CWinApp::CWinApp()
{
m_pCurrentWinApp=this;
cout<<"CWinApp Constructor"<<endl;
}
CWinApp::~CWinApp()
{
cout<<"CWinApp Destructor"<<endl;
}
};
class CDocument:public CCmdTarget
{
public:
CDocument::CDocument(){cout<<"CDocument Constructor"<<endl;}
CDocument::~CDocument(){cout<<"CDocument Destructor"<<endl;}
};
class CWnd:public CCmdTarget
{
public:
CWnd::CWnd(){cout<<"CWnd Constructor"<<endl;}
CWnd::~CWnd(){cout<<"CWnd Destructor"<<endl;}
};
class CFrameWnd:public CWnd
{
public:
CFrameWnd::CFrameWnd(){cout<<"CFrameWnd Constructor"<<endl;}
CFrameWnd::~CFrameWnd(){cout<<"CFrameWnd Destructor"<<endl;}
};
class CView:public CWnd
{
public:
CView::CView(){cout<<"CView Constructor"<<endl;}
CView::~CView(){cout<<"CView Destructor"<<endl;}
};
CWinApp* AfxGetApp();
MFC.cpp
#include "StdAfx.h"
#include "my.h"
extern CMyWinApp theApp;
CWinApp* AfxGetApp()
{
return theApp.m_pCurrentWinApp;
}
my.h
#include<iostream>
#include"MFC.h"
class CMyWinApp:public CWinApp
{
public:
CMyWinApp::CMyWinApp(){cout<<"CMyWinApp Constructor"<<endl;}
CMyWinApp::~CMyWinApp(){cout<<"CMyWinApp Destructor"<<endl;}
};
class CMyFrameWnd:public CFrameWnd
{
public:
CMyFrameWnd(){cout<<"CMyFrameWnd Constructor"<<endl;}
~CMyFrameWnd(){cout<<"CMyFrameWnd Destructor"<<endl;}
};
main 函数
#include "stdafx.h"
#include "my.h"
#include"stdlib.h"
CMyWinApp theApp;//global object
int _tmain(int argc, _TCHAR* argv[])
{
CWinApp* pApp=AfxGetApp();
system("pause");
return 0;
}
结果: