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;
}
}