运行时对象类型识别(RTTI)

Eddy 发布于2010-7-20 13:7:1 分类: 程序设计 已浏览loading 网友评论0条 我要评论

类信息表CRuntimeClass:

数据成员

m_lpszClassName    The name of the class.

m_nObjectSize    The size of the object in bytes.

m_pBaseClass    A pointer to the CRuntimeClass structure of the base class.

m_pfnCreateObject    A pointer to the function that dynamically creates the object.

m_pfnGetBaseClass    Returns the CRuntimeClass structure (only available when dynamically linked).

m_wSchema    The schema number of the class.

Operations
CreateObject    Creates an object during run time.
 
FromName        Creates an object during run time using the familiar class name.
 
IsDerivedFrom   Determines if the class is derived from the specified class.

例子:设计一个含有类信息表的类Myclass的控制台程序,在程序中通过Myclass对象m获得其所属类名:

#include <iostream.h>
//类信息表结构(简化-只保留了类名域)
struct CRuntimeClass
{
 char* m_lpszClassName;
};
//Myclass及实现(简化)
class Myclass
{
public:
 static CRuntimeClass classMyclass;//定义变量作为类信息表
 static CRuntimeClass* GetRuntimeClass();//获得里类信息表指针
};
//填写类信息表
CRuntimeClass Myclass::classMyclass={"Myclass"};
CRuntimeClass* Myclass::GetRuntimeClass()
{
 return &Myclass::classMyclass;
}
//主函数
void main()
{
 Myclass m;
 CRuntimeClass* p=m.GetRuntimeClass();
 cout<<"对象m所属类名为:"<<p->m_lpszClassName<<endl;
}

已经有(0)位网友发表了评论,你也评一评吧!
原创文章如转载,请注明:转载自Eddy Blog
原文地址:http://www.rrgod.com/program/446.html     欢迎订阅Eddy Blog

关于 RTTI  运行时  对象类型  识别  的相关文章

记住我的信息,下次不用再输入 欢迎给Eddy Blog留言