- struct CRuntimeClass
- {
-
- LPCSTR m_lpszClassName; //-------------类型识别宏用到
- int m_nObjectSize;
- UINT m_wSchema;
- CObject* (PASCAL* m_pfnCreateObject)();
- #ifdef _AFXDLL
- CRuntimeClass* (PASCAL* m_pfnGetBaseClass)();
- #else
- CRuntimeClass* m_pBaseClass;//-------------类型识别宏用到
- #endif
-
- CObject* CreateObject();//-------------动态创建宏用到
- ...
- };
类型识别宏 用来建立类型识别网,通过它可以知道继承关系
DECLARE_DYNAMIC(class_name)
IMPLEMENT_DYNAMIC(class_name, base_class_name)
struct CRuntimeClass
{
...
LPCSTR m_lpszClassName; //类名
CRuntimeClass* m_pBaseClass;//基类名
...
}
- class CMainFrame : public CMDIFrameWnd
- {
- DECLARE_DYNAMIC(CMainFrame)
- ...
- }
- IMPLEMENT_DYNAMIC(CMainFrame, CMDIFrameWnd)
- ...
- class CMainFrame : public CMDIFrameWnd
- {
- public:
- static const CRuntimeClass classCMainFrame;
- virtual CRuntimeClass* GetRuntimeClass() const;
- ...
- }
- AFX_COMDAT const CRuntimeClass CMainFrame::classCMainFrame =
- {CMainFrame,sizeof(classCMainFrame ),0xFFFF,NULL,(CRuntimeClass*)(&CMDIFrameWnd::classCMDIFrameWnd),NULL,NULL};
- CRuntimeClass* CMainFrame::GetRuntimeClass() const
- { return &CMainFrame::classCMainFrame; }
动态创建宏 其实就是多实现了CreateObject函数
(注意此时类构造函数为protected类型,所以其实例只能通过CreateObject来构造)
DECLARE_DYNCREATE(class_name)
IMPLEMENT_DYNCREATE(class_name, base_class_name)
struct CRuntimeClass
{
...
CObject* (PASCAL* m_pfnCreateObject)(); //指向CreateObject的函数指针
CObject* CreateObject();
...
};
- class CMainFrame
- {
- public:
- static const CRuntimeClass classCMainFrame;
- virtual CRuntimeClass* GetRuntimeClass() const;
- static CObject* PASCAL CreateObject();
- ...
- };
- CObject* PASCAL CMainFrame::CreateObject() { return new classCMainFrame; }
- AFX_COMDAT const CRuntimeClass CMainFrame::classCMainFrame =
- {CMainFrame,sizeof(classCMainFrame),0xFFFF,CMainFrame::CreateObject,(CRuntimeClass*)(&CMDIFrameWnd::classCMDIFrameWnd),NULL,NULL};
- CRuntimeClass* CMainFrame::GetRuntimeClass() const
- {
- return &CMainFrame::classCMainFrame;
- }
摘自
http://www.chinaitpower.com/A/2001-10-06/880.html