Dynamically created in MFC

1, in order to be able to dynamically create the program must maintain a CRuntimeClass class, when you need to identify a class, in order to find on this list , and this is through a set of macros to achieve. Because it is a linked list, so we need to initialize, this initialization CObject with slightly different macros to achieve.

2, CRuntimeClass structure:

 

LPCSTR m_lpszClassName //类名

 

int m_nObjectSize // byte size class, allocated memory is not calculated

UINT m_wSchema // generally 0xFFFF

 

CObject * (PASCAL * m_pfnCreateObject) () // a function pointer, to dynamically create objects

 

CRuntimeClass * (PASCAL * m_pfn_GetBaseClass) () // if it is dynamically loaded, then it is a pointer to an object base class CRuntimeClass, otherwise null

 

CRuntimeClass * m_pBaseClass // If static load, then it is a pointer to an object CRuntimeClass base class, otherwise null

 

 

 

CObject * CreateObject (); // a function used to create the class itself

 

 

BOOL IsDerivedFrom (const CRuntimeClass * pBaseClass) const; // a function for judging whether a class inherits from

3, this set of macros is DECLARE_DYNCREATE (class_name) and IMPLEMENT_DYNCREATE (class_name, base_class_name), in fact, this set of macros inside there is a set of macros, you can go to see, in fact, there's this set of macros is to achieve the type of recognition. The following will show the code after this set of macro expansion should be how:

#define DECLARE_DYNCREATE(class_name)所对应的代码

protected: 
	static CRuntimeClass* PASCAL _GetBaseClass(); 
public: 
	static const CRuntimeClass class##class_name; 
	static CRuntimeClass* PASCAL GetThisClass(); 
	virtual CRuntimeClass* GetRuntimeClass() const; 
static CObject* PASCAL CreateObject();


#define IMPLEMENT_DYNCREATE(class_name, base_class_name)所对应的代码

CObject* PASCAL class_name::CreateObject() 
		{ return new class_name; }
CRuntimeClass* PASCAL class_name::_GetBaseClass() 
		{ return RUNTIME_CLASS(base_class_name); } 
	AFX_COMDAT const CRuntimeClass class_name::class##class_name = { 
		#class_name, sizeof(class class_name), wSchema, pfnNew, 
			&class_name::_GetBaseClass, NULL, class_init }; 
	CRuntimeClass* PASCAL class_name::GetThisClass() 
		{ return _RUNTIME_CLASS(class_name); } 
	CRuntimeClass* class_name::GetRuntimeClass() const 
		{ return _RUNTIME_CLASS(class_name); }


假如这里有一个CView类
则	#define DECLARE_DYNCREATE(CView)对应的翻译后的代码为:

protected: 
	//其中PASCAL是调用协议,此处声明了一个函数。
	static CRuntimeClass* PASCAL _GetBaseClass();
 public: 
	//此处声明一个CRuntimeClass的对象,来保存相应的类信息
	static const CRuntimeClass classCView; 
	//声明一个获得类本身的信息的一个函数
	static CRuntimeClass* PASCAL GetThisClass(); 
	//声明一个获得CRuntimeClass的结构
	virtual CRuntimeClass* GetRuntimeClass() const; 
	//声明一个可以动态创建类本身的函数
	static CObject* PASCAL CreateObject();

#define IMPLEMENT_DYNCREATE(CView, CWnd)所对应翻译后的代码为:

//创建类本身的对象
CObject* PASCAL CView::CreateObject() 
{
	 return new CView;
 }
//获得基类的信息
CRuntimeClass* PASCAL CView::_GetBaseClass() 
{ 
	return RUNTIME_CLASS(base_class_name);
 } 
//声明一个结构对象,并初始化
AFX_COMDAT const CRuntimeClass CView::classCView = { 
	"CView", sizeof(class CView), 0xFFFF, CView::CreateObject, 
		&CView::_GetBaseClass, NULL, class_init }; 
//获得该类的相应信息
CRuntimeClass* PASCAL CView::GetThisClass() 
{
	 return _RUNTIME_CLASS(CView);
} 
获得该类的相应信息
CRuntimeClass* class_name::GetRuntimeClass() const 
{
	 return _RUNTIME_CLASS(CView);
 }

 

 

Reproduced in: https: //www.cnblogs.com/wang-can/p/3303520.html

Guess you like

Origin blog.csdn.net/weixin_34161083/article/details/94063567