栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

深入浅出MFC 书中源码Frame1(C++11)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

深入浅出MFC 书中源码Frame1(C++11)

深入浅出MFC 书中源码Frame1(C++11) MFC.h
#include 

class CObject
{
public:
	CObject() { std::cout << "CObject Constructor" << std::endl; }
	~CObject() { std::cout << "CObject Destructor" << std::endl; }
};

class CCmdTarget : public CObject
{
public:
	CCmdTarget() { std::cout << "CCmdTarget Constructor" << std::endl; }
	~CCmdTarget() { std::cout << "CCmdTarget Destructor" << std::endl; }
};

class CWinThread : public CCmdTarget
{
public:
	CWinThread() { std::cout << "CWinThread Constructor" << std::endl; }
	~CWinThread() { std::cout << "CWinThread Destructor" << std::endl; }
};

class CWinApp : public CWinThread
{
public:
	CWinApp* m_pCurrentWinApp;

public:
	CWinApp() { m_pCurrentWinApp = this; std::cout << "CWinApp Constructor" << std::endl; }
	~CWinApp() { std::cout << "CWinApp Destructor" << std::endl; }
};

class CDocument : public CCmdTarget
{
public:
	CDocument() { std::cout << "CDocument Constructor" << std::endl; }
	~CDocument() { std::cout << "CDocument Destructor" << std::endl; }
};


class CWnd : public CCmdTarget
{
public:
	CWnd() { std::cout << "CWnd Constructor" << std::endl; }
	~CWnd() { std::cout << "CWnd Destructor" << std::endl; }
};

class CFrameWnd : public CWnd
{
public:
	CFrameWnd() { std::cout << "CFrameWnd Constructor" << std::endl; }
	~CFrameWnd() { std::cout << "CFrameWnd Destructor" << std::endl; }
};

class CView : public CWnd
{
public:
	CView() { std::cout << "CView Constructor" << std::endl; }
	~CView() { std::cout << "CView Destructor" << std::endl; }
};


// global function

CWinApp* AfxGetApp();
MFC.cpp
#include "Frame1.h"

extern CMyWinApp theApp;

CWinApp* AfxGetApp()
{
	return theApp.m_pCurrentWinApp;
}
Frame1.h
#include 
#include "MFC.h"

class CMyWinApp : public CWinApp
{
public:
	CMyWinApp() { std::cout << "CMyWinApp Constructor" << std::endl; }
	~CMyWinApp() { std::cout << "CMyWinApp Destructor" << std::endl; }
};

class CMyFrameWnd : public CFrameWnd
{
public:
	CMyFrameWnd() { std::cout << "CMyFrameWnd Constructor" << std::endl; }
	~CMyFrameWnd() { std::cout << "CMyFrameWnd Destructor" << std::endl; }
};
Frame1.cpp
#include "Frame1.h"

CMyWinApp theApp;	// globasl object

//----------------------------------------
// main
//----------------------------------------
int main()
{
	CWinApp* pApp = AfxGetApp();

	return 0;
}
输出结果

CObject Constructor
CCmdTarget Constructor
CWinThread Constructor
CWinApp Constructor
CMyWinApp Constructor
CMyWinApp Destructor
CWinApp Destructor
CWinThread Destructor
CCmdTarget Destructor
CObject Destructor

类图 主要结论

1·注意历代父类构造函数和析构函数的调用顺序。
2·全局对象的构建比程序进入点要早,亦即是说,输出结果中所有构造函数输出操作都是在main函数之前完成的。
3·main 函数调用全局函数 AfxGetApp() 取得 theApp 对象指针,也是仿真 MFC 程序的手法。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/832804.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号