//main.h头文件
#pragma once
#include"a.h"
class CShape
{
public:virtual CRuntimeClassTest* GetRuntimeClassTest() const;BOOL IsKindOf(const CRuntimeClassTest* pClass) const;
public:static CRuntimeClassTest classCShape;
};static char szCShape[] = "CShape";
struct CRuntimeClassTest CShape::classCShape = { szCShape ,sizeof(szCShape),0xffff,NULL,NULL};
static CLASS_INIT init_CShape(&CShape::classCShape);CRuntimeClassTest* CShape::GetRuntimeClassTest() const
{return &CShape::classCShape;
}inline BOOL CShape::IsKindOf(const CRuntimeClassTest * pClass) const
{CRuntimeClassTest* pClassThis = GetRuntimeClassTest();while (pClassThis!= NULL){if (pClassThis == pClass)return TRUE;pClassThis = pClassThis->m_pBaseClass;}return FALSE;
}
//a.h头文件
#pragma once
#include<afxwin.h>
class CShape;
//RTTI结构体
struct CRuntimeClassTest
{LPCSTR m_lpszClassName;int m_nObjectSize;UINT m_wSchema;CShape*(PASCAL*m_pfnCreateShape)();CRuntimeClassTest* m_pBaseClass;static CRuntimeClassTest* pFirstClass;CRuntimeClassTest* m_pNextClass;
};struct CLASS_INIT
{//c++struct也有构造函数CLASS_INIT(CRuntimeClassTest* pNewClass);
};//下面为几组宏,由于文字代换
/*
*宏定义,"\"指换行,后面的语句和它是一个部分的。
*其中##告诉编译器把左右连个字符串连接在一起,即把class和参数class_name的值连接在一起。
*/
#define TyPeName(class_name)\public:\static CRuntimeClassTest class##class_name;\virtual CRuntimeClassTest* GetRuntimeClassTest() const;#define RUN_TIME_CLASS(class_name)\(&class_name::class##class_name)#define DYNAMIC_RUNTIMECLASS(class_name,baseclass_name,wSchema,pfnNew)\static char lpsz##class_name[]=#class_name;\CRuntimeClassTest class_name::class##class_name={\lpsz##class_name,sizeof(class_name),wSchema,pfnNew,RUN_TIME_CLASS(baseclass_name),NULL};\static CLASS_INIT init_##class_name(&class_name::class_name::class##class_name);\CRuntimeClassTest* class_name::GetRuntimeClassTest() const\{return &class_name::class##class_name;}/*
*用于动态创建对象的宏
*/
#define DYNAMIC_CREATE(class_name,baseclass_name)\DYNAMIC_RUNTIMECLASS(class_name,baseclass_name,0xFFFF,NULL)CLASS_INIT::CLASS_INIT(CRuntimeClassTest * pNewClass)
{pNewClass->m_pNextClass = CRuntimeClassTest::pFirstClass;CRuntimeClassTest::pFirstClass = pNewClass;
}
//main.cpp
#pragma once
#include"a.h"
#include"main.h"
#include<iostream>
using namespace std;CRuntimeClassTest* CRuntimeClassTest::pFirstClass = NULL;//静态成员变量初始化的位置class CCircle :public CShape
{TyPeName(CCircle)};
class CEllipse :public CCircle
{TyPeName(CEllipse)
};DYNAMIC_CREATE(CCircle,CShape)
DYNAMIC_CREATE(CEllipse,CCircle)void Print()
{CRuntimeClassTest* pClass;for (pClass = CRuntimeClassTest::pFirstClass; pClass != NULL; pClass = pClass->m_pNextClass){cout << pClass->m_lpszClassName << "," << pClass->m_nObjectSize << "," << pClass->m_wSchema << endl;}
}
int main()
{CEllipse* pE = new CEllipse;CCircle* pC = new CCircle;Print();if (pE->IsKindOf(RUN_TIME_CLASS(CEllipse))){cout << "对象pE的类型为:CEllipse" << endl;}if (pC->IsKindOf(RUN_TIME_CLASS(CShape))){cout << "对象pC的类型为CShape" << endl;}delete pE;delete pC;return 0;
}
若代码改为如下图,则运行结果如下:
if (pE->IsKindOf(RUN_TIME_CLASS(CEllipse))){cout << "对象pE的类型为:CEllipse" << endl;}if (pC->IsKindOf(RUN_TIME_CLASS(CEllipse))){cout << "对象pC的类型为CShape" << endl;}