#include <iostream>
#include<string>
using namespace std;
class CObject;
///类信息表结构
struct CRuntimeClass
{
char* m_lpszClassname ; //类名
CObject* (_stdcall *m_pfnCreateObject)(); ///对象构建函数指针
CObject* _stdcall CreateObject();
///对象构建函数的声明
};
//CObject类声明及实现
class CObject
{
public :
virtual void SayHello() {cout<<"Hello CObject \n";}
};
///派生类Myclass及实现
class Myclass : public CObject
{
public:
virtual void SayHello() {cout<<"Hello Myclass\n";}
public:
///对象动态创建的声明
static CRuntimeClass classMyclass;
//定义CRuntimeClass变量作为信息表
static CRuntimeClass *GetRuntimeClass();//获得信息表指针的函数
};
///动态创建的实现
///对象构建函数的实现
CObject* _stdcall CreateObject()
{
return new Myclass;
}
///填写信息表
CRuntimeClass Myclass::classMyclass = {"Myclass" , CreateObject};
///获得类信息表指针函数的实现
CRuntimeClass * Myclass::GetRuntimeClass()
{
return &Myclass::classMyclass ;
}
///主函数
void main()
{
char _lpszCLS[10];
cout<<"Myclass"<<endl;
cout<<"请输入类名: ";
cin>>_lpszCLS;
CRuntimeClass * p = Myclass::GetRuntimeClass ();
if(!strcmp(p->m_lpszClassname , _lpszCLS))
{
CObject* _stdcall pp = p->m_pfnCreateObject();
pp->SayHello ();
}
else
{
cout<<"NO"<<endl;
}
}