MFC RUNTIMECLASS

struct CRuntimeClass
{
// Attributes
 LPCSTR m_lpszClassName;
 int m_nObjectSize;
 UINT m_wSchema; // schema number of the loaded class
 CObject* (PASCAL* m_pfnCreateObject)(); // NULL => abstract class
#ifdef _AFXDLL
 CRuntimeClass* (PASCAL* m_pfnGetBaseClass)();
#else
 CRuntimeClass* m_pBaseClass;
#endif

// Operations
 CObject* CreateObject();
 BOOL IsDerivedFrom(const CRuntimeClass* pBaseClass) const;

// Implementation
 void Store(CArchive& ar) const;
 static CRuntimeClass* PASCAL Load(CArchive& ar, UINT* pwSchemaNum);

 // CRuntimeClass objects linked together in simple list
 CRuntimeClass* m_pNextClass;       // linked list of registered classes
};

CRUNTIMECLASS 是一个结构体

#define DECLARE_DYNAMIC(class_name) /
public: /
    static CRuntimeClass class##class_name; /
virtual CRuntimeClass* GetRuntimeClass() const;

DECLARE_DYNAMIC 主要是声明一个静态的本类的CRUNTIMECLASS 对象以及一个得到这个对象的GET函数。

#define IMPLEMENT_DYNAMIC(class_name, base_class_name) /
_IMPLEMENT_RUNTIMECLASS(class_name, base_class_name, 0xFFFF, NULL)

IMPLEMENT_DYNAMIC主要是定义另外一个宏_IMPLEMENT_RUNTIMECLASS,通过这个宏来做一些对于CRUNTIMECLASS对象的初始化工作。
#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), wSchema, pfnNew, /
    RUNTIME_CLASS(base_class_name), NULL }; /
    static AFX_CLASSINIT _init_##class_name(&class_name::class##class_name); /
    CRuntimeClass* class_name::GetRuntimeClass() const /
    { return &class_name::class##class_name; } /

_IMPLEMENT_RUNTIMECLASS则是对先前声明的静态对象的初始化,将本类的CRUNTIMECLASS 与父类的CRUNTIMECLASS对象建立起链式关系,同时实现声明中的GET函数。

  #define RUNTIME_CLASS(class_name) 
    (&class_name::class##class_name)

RUNTIME_CLASS  宏则是实现类名到CRUNTIMECLASS之间的转换,相当于一个函数,但是返回的是生成对象的地址。

    struct AFX_CLASSINIT
    { AFX_CLASSINIT(CRuntimeClass* pNewClass); };

    AFX_CLASSINIT::AFX_CLASSINIT(CRuntimeClass* pNewClass)
    {
        pNewClass->m_pNextClass = CRuntimeClass::pFirstClass;
        CRuntimeClass::pFirstClass = pNewClass;
    }

这个结构体在构造函数中将新类的CRUNTIMECLASS对象串联到CRUNTIMECLASS在类层次中的链式结构中。

pFirstClass ->pNewClass-->pBaseClass-->pBaseBaseClass...-->pObject

 

RTTI便是在这个链中寻找合适的CRUNTIMECLASS与当前对象的CRUNTIMECLASS对象进行匹配,继而确定其运行时类型。

 

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值