//a.h头文件
#pragma once
#include"m.h"
#include<iostream>
using namespace std;
class CShape;//类声明
//RTTI结构体
struct CRuntimeClassTest
{
LPCSTR m_lpszClassName;
int m_nObjectSize;
UINT m_wSchema;
CShape*(PASCAL*m_pfnCreateShape)();//PASCAL宏为 __stdcall,一种回调机制
CRuntimeClassTest* m_pBaseClass;
static CRuntimeClassTest* pFirstClass;
CRuntimeClassTest* m_pNextClass;
CShape* CreateObject();
static CRuntimeClassTest* PASCAL Load();
};
//中间结构定义
struct CLASS_INIT_TEST
{
CLASS_INIT_TEST(CRuntimeClassTest* pNewClass);//c++struct也有构造函数
};
//CShape基类定义
class CShape
{
public:
virtual CRuntimeClassTest* GetRuntimeClassTest() const;
BOOL IsKindOf(const CRuntimeClassTest* pClass) const;
virtual void InputMsg() { cout << "CShape Successfully Created!" << endl; }
public:
static CRuntimeClassTest classCShape;
};
//a.cpp函数实现文件
#include "a.h"
#include"m.h"
#include<iostream>
using namespace std;
//全局变量
static char szCShape[] = "CShape";
struct CRuntimeClassTest CShape::classCShape = { szCShape ,sizeof(szCShape),0xffff,NULL,NULL };
static CLASS_INIT_TEST init_CShape(&CShape::classCShape);
//CLASS_INIT_TEST结构体成员函数实现
CLASS_INIT_TEST::CLASS_INIT_TEST(CRuntimeClassTest * pNewClass)
{
pNewClass->m_pNextClass = CRuntimeClassTest::pFirstClass;
CRuntimeClassTest::pFirstClass = pNewClass;
}
//Shape成员函数实现
CRuntimeClassTest* CShape::GetRuntimeClassTest() const
{
return &CShape::classCShape;
}
BOOL CShape::IsKindOf(const CRuntimeClassTest * pClass) const
{
CRuntimeClassTest* pClassThis = GetRuntimeClassTest();
while (pClassThis != NULL)
{
if (pClassThis == pClass)
return TRUE;
pClassThis = pClassThis->m_pBaseClass;
}
return FALSE;
}
//CRuntimeClassTest结构的函数实现
CShape * CRuntimeClassTest::CreateObject()
{
if (m_pfnCreateShape == NULL)
{
cout << "Error:Trying to creat object which is not DYNAMIC_CREATE_OBJECT:" << m_lpszClassName << endl;;
return NULL;
}
CShape* pS = NULL;
pS = (*m_pfnCreateShape)();
return pS;
}
CRuntimeClassTest * CRuntimeClassTest::Load()
{
char szClassName[64];
CRuntimeClassTest* pClass;
cout << "Input a class name:";
cin >> szClassName;
for (pClass = pFirstClass; pClass != NULL; pClass = pClass->m_pNextClass)
{
if (strcmp(szClassName, pClass->m_lpszClassName) == 0)//strcmp比较两字符串是否相等
return pClass;
}
cout << "class not found:" << szClassName << endl;
return NULL;
}
//m.h宏定义文件
#pragma once
#include<afxwin.h>
#include"a.h"
/*
下面为几组宏,由于文字代换
*宏定义,"\"指换行,后面的语句和它是一个部分的。
*其中##告诉编译器把左右连个字符串连接在一起,即把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_TEST 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)
//关键技术三,动态创建对象需要定义的宏
#define DYNAMIC_CREATE_OBJECT(class_name)\
TyPeName(class_name)\
static CShape*PASCAL CreateObject();
#define IMP_DYN_CREATE(class_name,baseclass_name)\
CShape*PASCAL class_name::CreateObject(){return new class_name;}\
DYNAMIC_RUNTIMECLASS(class_name,baseclass_name,0xffff,class_name::CreateObject)
//main.cpp测试类编写与测试
#pragma once
#include"a.h"
#include"m.h"
#include<iostream>
using namespace std;
CRuntimeClassTest* CRuntimeClassTest::pFirstClass = NULL;//静态成员变量初始化的位置
class CEllipse :public CShape
{
//TyPeName(CEllipse)
DYNAMIC_CREATE_OBJECT(CEllipse)
public:
CEllipse() { cout << "CEllipse Successfully Constructor!" << endl; }
void InputMsg() { cout << "CEllipse Successfully Created!" << endl; }
};
class CCircle :public CEllipse
{
//TyPeName(CCircle)
DYNAMIC_CREATE_OBJECT(CCircle)
public:
CCircle() { cout << "CCircle Successfully Constructor!"<< endl; }
void InputMsg() { cout << "CCircle Successfully Created!" << endl; }
};
//DYNAMIC_CREATE(CEllipse,CShape)
//DYNAMIC_CREATE(CCircle, CEllipse)
IMP_DYN_CREATE(CEllipse, CShape)
IMP_DYN_CREATE(CCircle, CEllipse)
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;
}
else
{
cout << "对象pE的类型不是CEllipse." << endl;
}
if (pC->IsKindOf(RUN_TIME_CLASS(CEllipse)))
{
cout << "对象pC的类型为CEllipse." << endl;
}
else
{
cout << "对象pC的类型不是CEllipse." << endl;
}
if (pE->IsKindOf(RUN_TIME_CLASS(CCircle)))
{
cout << "对象pE的类型为:CCircle." << endl;
}
else
{
cout << "对象pE的类型不是CCircle." << endl;
}
if (pC->IsKindOf(RUN_TIME_CLASS(CCircle)))
{
cout << "对象pC的类型为CCircle." << endl;
}
else
{
cout << "对象pC的类型不是CCircle." << endl;
}
delete pE;
delete pC;
//测试动态创建类对象
CRuntimeClassTest* pClassCreate;
CShape* pShape;
while (true)
{
if ((pClassCreate = CRuntimeClassTest::Load())== NULL)
break;
pShape = pClassCreate->CreateObject();
if (pShape != NULL)
pShape->InputMsg();
}
return 0;
}
运行结果如下,从运行结果可知,动态创建类对象成功。