最最基本的框架宏使用RUNTIME_CLASS DECLARE_DYNAMIC IMPLEMENT_RUNTIMECLASS

Demo.h 

// 简化封装
// 部分的内容在MFC中都是固化的,所以理解并加以记忆是必要的
//
#pragma once
#include <windows.h>


///
// 调试支持
#define AfxDebugBreak()    _asm { int 3 }
#ifdef _DEBUG

void __cdecl AfxTrace(LPCTSTR lpszFormat, ...);

#define TRACE              ::AfxTrace

#define ASSERT(f)			\
	if (!(f))				\
		AfxDebugBreak();

#define VERIFY(f)          ASSERT(f)

#else // _DEBUG

#define ASSERT(f)          ((void)0)   
#define VERIFY(f)          ((void)(f))
inline void __cdecl AfxTrace(LPCTSTR, ...) { }
#define TRACE              (void)0

#endif // _DEBUG

// 宏定义

// class_name前必须有 #
#define IMPLEMENT_RUNTIMECLASS(class_name, base_class_name, wSchema, pfnNew)	\
	const CRuntimeClass class_name::class##class_name =							\
	{									\
		#class_name,					\
		sizeof(class class_name),		\
		wSchema,						\
		pfnNew,							\
		RUNTIME_CLASS(base_class_name),	\
		NULL							\
	};									\
	CRuntimeClass* class_name::GetRuntimeClass() const { return RUNTIME_CLASS(class_name); }

// RUNTIME_CLASS宏用来取得class_name类中CRuntimeClass结构的地址
#define RUNTIME_CLASS(class_name) ((CRuntimeClass*)&class_name::class##class_name)

// no支持动态创建的宏
#define DECLARE_DYNAMIC(class_name)						\
public:													\
	static const CRuntimeClass class##class_name;		\
	virtual CRuntimeClass* GetRuntimeClass() const; 

#define IMPLEMENT_DYNAMIC(class_name, base_class_name) \
IMPLEMENT_RUNTIMECLASS(class_name, base_class_name, 0xFFFF, NULL)

// 支持动态创建的宏
#define DECLARE_DYNCREATE(class_name)		\
	DECLARE_DYNAMIC(class_name)				\
	static CObject* __stdcall CreateObject();

#define IMPLEMENT_DYNCREATE(class_name, base_class_name)		\
	CObject* __stdcall class_name::CreateObject()				\
	{ return new class_name; }									\
	IMPLEMENT_RUNTIMECLASS(class_name, base_class_name, 0xFFFF,class_name::CreateObject)


class CObject;

struct CRuntimeClass
{
	// 属性(Attributes)
	LPCSTR m_lpszClassName;      // 类的名称
	int    m_nObjectSize;        // 类的大小
	UINT   m_wSchema;            // 类的版本号
	CObject* (__stdcall* m_pfnCreateObject)();  // 创建类的函数的指针
	CRuntimeClass* m_pBaseClass;                // 其基类中CRuntimeClass结构的地址
	
	// 操作(operations)
	CObject* CRuntimeClass::CreateObject()
	{
		if(m_pfnCreateObject == NULL)
			return NULL;
		return (*m_pfnCreateObject)(); 
	}
	BOOL CRuntimeClass::IsDerivedFrom(const CRuntimeClass* pBaseClass) const
	{
		const CRuntimeClass* pClassThis = this;
		while(pClassThis != NULL)
		{
			if(pClassThis == pBaseClass) // 判断标识类的CRuntimeClass的首地址是否相同
				return TRUE;
			pClassThis = pClassThis->m_pBaseClass;
		}
		return FALSE; 
	}

	CRuntimeClass* m_pNextClass;  // 将所有CRuntimeClass对象用简单链表连在一起
};

class CObject
{
public:
	virtual CRuntimeClass* CObject::GetRuntimeClass() const
	{
		return RUNTIME_CLASS(CObject);
	}
	inline CObject::~CObject() { }
	
	// 属性(Attibutes)
public:
	BOOL CObject::IsKindOf(const CRuntimeClass* pClass) const
	{
		CRuntimeClass* pClassThis = GetRuntimeClass();
		return pClassThis->IsDerivedFrom(pClass);
	}
	
	// 实现(implementation)
public:
	static const CRuntimeClass classCObject;
};

const struct CRuntimeClass CObject::classCObject = 	{ "CObject", sizeof(CObject), 0xFFFF,NULL, NULL, NULL};

</pre><pre>

Demo.cpp 

// 
// 多多调试
// 为了理解MFC的框架
// 了解几个类细微的不同之处
//
#include "Demo.h"

class CPersonA : public CObject
{
public: 
	static const CRuntimeClass classCPersonA;
	virtual CRuntimeClass* GetRuntimeClass() const{ return RUNTIME_CLASS(CPersonA); } 
};
const CRuntimeClass CPersonA::classCPersonA = {"CPersonA", sizeof(CPersonA), 0xFFFF, NULL,RUNTIME_CLASS(CObject), NULL };

class CPersonB : public CObject
{
public:
	static const CRuntimeClass classCPersonB;
	virtual CRuntimeClass* GetRuntimeClass() const{ return RUNTIME_CLASS(CPersonB); } 
	static CObject* __stdcall CreateObject()      { return new CPersonB; }
};
const CRuntimeClass CPersonB::classCPersonB = {"CPersonB", sizeof(CPersonB), 0xFFFF, NULL,RUNTIME_CLASS(CObject), NULL };

class CPersonEA : public CObject
{
	DECLARE_DYNAMIC(CPersonEA)
};
IMPLEMENT_DYNAMIC(CPersonEA, CObject)

class CPersonEB : public CObject
{
	DECLARE_DYNCREATE(CPersonEB)
};
IMPLEMENT_DYNCREATE(CPersonEB, CObject)

void func1();void func2();void func3();void func4();

void main()
{
	func1();
	func2();
	func3();
	func4();
}

void func1()
{
	CObject* pMyObject = new CPersonA;	
	if(pMyObject->IsKindOf(RUNTIME_CLASS(CPersonA))){
		CPersonA* pMyPerson = (CPersonA*)pMyObject;
		delete pMyPerson;
	}
	else{
		delete pMyObject;
	}
}
void func2()
{
	CRuntimeClass* pRuntimeClass = RUNTIME_CLASS(CPersonB);
	
	// 取得了pRuntimeClass指针,不用知道类的名字就可以创建该类
	CObject* pObject = pRuntimeClass->CreateObject();
	if(pObject != NULL && pObject->IsKindOf(RUNTIME_CLASS(CPersonB))){
		delete pObject;
	}
}
void func3()
{
	CObject* pMyObject = new CPersonEA;	
	if(pMyObject->IsKindOf(RUNTIME_CLASS(CPersonEA))){
		CPersonEA* pMyPerson = (CPersonEA*)pMyObject;
		delete pMyPerson;
	}
	else{
		delete pMyObject;
	}
}
void func4()
{
	CRuntimeClass* pRuntimeClass = RUNTIME_CLASS(CPersonEB);
	
	// 取得了pRuntimeClass指针,不用知道类的名字就可以创建该类
	CObject* pObject = pRuntimeClass->CreateObject();
	if(pObject != NULL && pObject->IsKindOf(RUNTIME_CLASS(CPersonEB))){
		delete pObject;
	}
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值