动态生成,即在程序运行时根据类型名称,动态的生成符合该类型的对象。
1、对CRuntimeClass的扩充
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
CRuntimeClass* m_pBaseClass;
CObject* CreateObject();//指向对象建立函数的指针
static CRuntimeClass* PASCAL Load();
// CRuntimeClass objects linked together in simple list
static CRuntimeClass* pFirstClass; // start of class list
CRuntimeClass* m_pNextClass; // linked list of registered classes
};
2、DECLARE_DYNCREATE/IMPLEMENT_DYNCREATE宏
#define DECLARE_DYNCREATE(class_name) /
DECLARE_DYNAMIC(class_name) /
static CObject* PASCAL CreateObject();
#define IMPLEMENT_DYNCREATE(class_name, base_class_name) /
CObject* PASCAL class_name::CreateObject() /
{ return new class_name; } /
//动态创建其实很简单:return new class_name
_IMPLEMENT_RUNTIMECLASS(class_name, base_class_name, 0xFFFF, /
class_name::CreateObject)