struct CRuntimeClass
{
// Attributes
LPCSTR m_lpszClassName;
int m_nObjectSize;
UINT m_wSchema; // schema number of the loaded class
CObject* (PASCAL* m_pfnCreateObject)(); // NULL => abstract class
#ifdef _AFXDLL
CRuntimeClass* (PASCAL* m_pfnGetBaseClass)();
#else
CRuntimeClass* m_pBaseClass;
#endif
// Operations
CObject* CreateObject();
BOOL IsDerivedFrom(const CRuntimeClass* pBaseClass) const;
// Implementation
void Store(CArchive& ar) const;
static CRuntimeClass* PASCAL Load(CArchive& ar, UINT* pwSchemaNum);
// CRuntimeClass objects linked together in simple list
CRuntimeClass* m_pNextClass; // linked list of registered classes
};
CRUNTIMECLASS 是一个结构体
#define DECLARE_DYNAMIC(class_name) /
public: /
static CRuntimeClass class##class_name; /
virtual CRuntimeClass* GetRuntimeClass() const;
DECLARE_DYNAMIC 主要是声明一个静态的本类的CRUNTIMECLASS 对象以及一个得到这个对象的GET函数。
#define IMPLEMENT_DYNAMIC(class_name, base_class_name) /
_IMPLEMENT_RUNTIMECLASS(class_name, base_class_name, 0xFFFF, NULL)
IMPLEMENT_DYNAMIC主要是定义另外一个宏_IMPLEMENT_RUNTIMECLASS,通过这个宏来做一些对于CRUNTIMECLASS对象的初始化工作。
#define _IMPLEMENT_RUNTIMECLASS(class_name, base_class_name,wSchema,pfnNew) /
static char _lpsz##class_name[] = #class_name; /
CRuntimeClass class_name::class##class_name = { /
_lpsz##class_name, sizeof(class_name), wSchema, pfnNew, /
RUNTIME_CLASS(base_class_name), NULL }; /
static AFX_CLASSINIT _init_##class_name(&class_name::class##class_name); /
CRuntimeClass* class_name::GetRuntimeClass() const /
{ return &class_name::class##class_name; } /
_IMPLEMENT_RUNTIMECLASS则是对先前声明的静态对象的初始化,将本类的CRUNTIMECLASS 与父类的CRUNTIMECLASS对象建立起链式关系,同时实现声明中的GET函数。
#define RUNTIME_CLASS(class_name)
(&class_name::class##class_name)
RUNTIME_CLASS 宏则是实现类名到CRUNTIMECLASS之间的转换,相当于一个函数,但是返回的是生成对象的地址。
struct AFX_CLASSINIT
{ AFX_CLASSINIT(CRuntimeClass* pNewClass); };
AFX_CLASSINIT::AFX_CLASSINIT(CRuntimeClass* pNewClass)
{
pNewClass->m_pNextClass = CRuntimeClass::pFirstClass;
CRuntimeClass::pFirstClass = pNewClass;
}
这个结构体在构造函数中将新类的CRUNTIMECLASS对象串联到CRUNTIMECLASS在类层次中的链式结构中。
pFirstClass ->pNewClass-->pBaseClass-->pBaseBaseClass...-->pObject
RTTI便是在这个链中寻找合适的CRUNTIMECLASS与当前对象的CRUNTIMECLASS对象进行匹配,继而确定其运行时类型。