MFC为了达到RTTI的功能实现使用了CRunTimeClass类,该类记录必要的信息,以便建立型录,用链表来实现,CRunTimeClass的成员变量类的名称,链表的First指针和Next指针。 MFC为了把CRunTimeClass放进类中,就主要使用了DELCLARE_DYNAMIC和IMPLEMENT_DYNAMIC宏。 #define RUNTIME_CLASS(class_name)(&class_name::class##class_name)其中class##class_name为类中 #define DECLARE_DYNAMIC(class_name) /admin管理员组文章数量:1636944
public: /
static CRuntimeClass class##class_name; /
virtual CRuntimeClass* GetRuntimeClass() const; CRuntimeClass结构体变量,##告诉编译器把class和class_name连接起来。 DECLARE_DYNAMIC作用:声明类中的静态CRuntimeClass变量,使这个类中的所有对象共享。以前学习到静态变量但是不知为何用,现在在这里用到了。 #define IMPLEMENT_DYNAMIC(class_name, base_class_name) ///这就是IMPLEMENT_DYNAMIC完成的功能
_IMPLEMENT_RUNTIMECLASS(class_name, base_class_name, 0xFFFF, NULL) 其中_IMPLEMENT_RUNTIMECLASEE也是一个宏。 #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)//大小, wSchem//版本号a, 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; } / 其中AFX_CLASSINIT是一个结构体,它表示一个构造函数。 struct AFX_CLASSINIT
{ AFX_CLASSINIT(CRuntimeClass* pNewClass); };//声明 AFX_CLASSINIT::AFX_CLASSINIT(CRuntimeClass* pNewClass)//定义
{
pNewClass->m_pNextClass = CRuntimeClass::pFirstClass;
CRuntimeClass::pFirstClass = pNewClass;
} 例子:在MFC中经常会用到这两个宏。 //在头文件 class CView : public CWnd { DECLARE_DYNAMIC(CView) //在MFC中实际是显示 DECLARE_DYNCREATE ... }; //在实现文件 IMPLEMENT_DYNAMIC(CView,CWnd) //在MFC中实际是显示 IMPLEMENT_DYNCREATE 将两个宏展开就可以简单建构数据并完成加入链表的工作。 总结于《深入浅出MFC》 此文章来自于【http://blog.csdn/huahuamoon/article/details/1953932】
本文标签: 含意完整VCDELCLAREDYNAMICIMPLEMENTDYNAMIC
版权声明:本文标题:VC中DELCLARE_DYNAMIC和IMPLEMENT_DYNAMIC宏的完整含意和用法 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/dongtai/1729234805a1191870.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论