RTTI means Runtime Type Information.
The compiler itself enable this functionality.
Now here just to imitate the mechalism referring to Inside MFC.
1. CRuntimeClass node to construct the link list.
struct CRuntimeClass
{
LPCSTR m_lpszClassName;
int m_nObjectSize;
UINT m_wSchema;
CObject* (PASCAL* m_pfnCreateObject)();
CRuntimeClass* m_pBaseClass;
CRuntimeClass* m_pNextClass;
static CRuntimeClass* pFirstClass;
}
2. DECLARE_DYNAMIC(class_Name) to declare in .h file.
#define DECLARE_DYNAMIC(class_name) /
public: /
static CRuntimeClass class##class_name; /
virtual CRuntimeClass* GetRuntimeClass() const;
3. Implement macro in .cpp file
#define IMPLEMENT_DYNAMIC(class_name, base_class_name) /
_IMPLEMENT_RUNTIMECLASS(class_name, base_class_name, 0xFFFF, NULL)
#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), wShema,pfnNew, /
RuntimeClass(base_class_name),NULL }; /
static AFX_CLASSINIT _init_##class_name(&class_name::class##class_name); /
CRuntimeClass* class_name::GetRuntimeClass() const /
{return &clas_name::class##class_name; }
#define RUNTIME_CLASS(class_name) /
(&class_name::class##class_name)
struct AFX_CLASSINIT
{AFX_CLASSINIT(CRuntimeClass* pNewClass); };
3. The root class CObject has the specific definition as its m_pBaseClass = NULL.
4. Initialize the static variable.
CRuntimeClass* CRuntimeClass::pFirstClass = NULL;
5. Initial Class to link all the nodes.
AFX_CLASSINIT::AFX_CLASSINIT(CRuntimeClass* pNewClass)
{
pNewClass->m_pNextClass = CRuntimeClass::pFirstClass;
CRuntimeClass::pFirstClass = pNewClass;
}
6. Define the judge function in CObject IsKindof()
BOOL CObject:: IsKindOf(const CRuntimeClass* pClass) const
{
CRuntimeClass* pClassThis = GetRuntimeClass();
while (pClassThis != null)
{
if (pClassThis == pClass)
return True;
pClassThis = pClassThis->m_pBaseClass;
}
return FALSE;
}
The compiler itself enable this functionality.
Now here just to imitate the mechalism referring to Inside MFC.
1. CRuntimeClass node to construct the link list.
struct CRuntimeClass
{
LPCSTR m_lpszClassName;
int m_nObjectSize;
UINT m_wSchema;
CObject* (PASCAL* m_pfnCreateObject)();
CRuntimeClass* m_pBaseClass;
CRuntimeClass* m_pNextClass;
static CRuntimeClass* pFirstClass;
}
2. DECLARE_DYNAMIC(class_Name) to declare in .h file.
#define DECLARE_DYNAMIC(class_name) /
public: /
static CRuntimeClass class##class_name; /
virtual CRuntimeClass* GetRuntimeClass() const;
3. Implement macro in .cpp file
#define IMPLEMENT_DYNAMIC(class_name, base_class_name) /
_IMPLEMENT_RUNTIMECLASS(class_name, base_class_name, 0xFFFF, NULL)
#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), wShema,pfnNew, /
RuntimeClass(base_class_name),NULL }; /
static AFX_CLASSINIT _init_##class_name(&class_name::class##class_name); /
CRuntimeClass* class_name::GetRuntimeClass() const /
{return &clas_name::class##class_name; }
#define RUNTIME_CLASS(class_name) /
(&class_name::class##class_name)
struct AFX_CLASSINIT
{AFX_CLASSINIT(CRuntimeClass* pNewClass); };
3. The root class CObject has the specific definition as its m_pBaseClass = NULL.
4. Initialize the static variable.
CRuntimeClass* CRuntimeClass::pFirstClass = NULL;
5. Initial Class to link all the nodes.
AFX_CLASSINIT::AFX_CLASSINIT(CRuntimeClass* pNewClass)
{
pNewClass->m_pNextClass = CRuntimeClass::pFirstClass;
CRuntimeClass::pFirstClass = pNewClass;
}
6. Define the judge function in CObject IsKindof()
BOOL CObject:: IsKindOf(const CRuntimeClass* pClass) const
{
CRuntimeClass* pClassThis = GetRuntimeClass();
while (pClassThis != null)
{
if (pClassThis == pClass)
return True;
pClassThis = pClassThis->m_pBaseClass;
}
return FALSE;
}