本文介绍了此宏IMPLEMENT_DYNCREATE的宏定义表示什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


做什么此代码执行???

#define IMPLEMENT_DYNCREATE(class_name,base_class_name)\
CObject * PASCAL class_name :: CreateObject()\
{返回新的class_name; } \
#define IMPLEMENT_DYNCREATE(class_name,base_class_name)\
CObject * PASCAL class_name :: CreateObject()\
{返回新的class_name; } \
IMPLEMENT_RUNTIMECLASS(class_name,base_class_name,0xFFFF,\
class_name :: CreateObject,NULL)

what does the
this code performs???

#define IMPLEMENT_DYNCREATE(class_name, base_class_name) \
CObject* PASCAL class_name::CreateObject() \
{ return new class_name; } \
#define IMPLEMENT_DYNCREATE(class_name, base_class_name) \
CObject* PASCAL class_name::CreateObject() \
{ return new class_name; } \
IMPLEMENT_RUNTIMECLASS(class_name, base_class_name, 0xFFFF, \
class_name::CreateObject, NULL)

推荐答案

// In class Declaration (.h) 
class A : public CObject
{
	DECLARE_DYNCREATE( A )
};


//In class implementation( .cpp)
IMPLEMENT_DYNCREATE( A, CObject )



在运行时,它们会扩展并变为如下所示.这只是一个模拟,以了解其背后的概念



During run time these gets expanded and become as shown below. This is just a simulation to understand the concept behind it

In class Declaration (.h) 
class A : public CObject
{
	static CObject* PASCAL CreateObject();
protected:
	static CRuntimeClass* PASCAL _GetBaseClass();
public:
	static const AFX_DATA CRuntimeClass classA;
	virtual CRuntimeClass* GetRuntimeClass() const;
};
/* it declares 3 functions CreateObject(),GetBaseClass(),GetRuntimeClass() and one data member classA of type CRuntimeClass structure*/


//In class implementation( .cpp)

// Remember classA is static. so initialized as below
A::classA.m_lpszClassName = "A";
A::classA.m_nObjectSize = sizeof( A );
A::classA.m_wSchema = 0xFFFF;
A::classA.m_pfnCreateObject = A::CreateObject; // Function pointer
A::classA.m_pfnGetBaseClass =A:: _GetBaseClass; // Function pointer
A::m_pNextClass = NULL;

CObject* PASCAL A::CreateObject() 
{ 
return new A;
} 
CRuntimeClass* A::GetRuntimeClass() const 	
{ 
return RUNTIME_CLASS( A ); 
}
CRuntimeClass* PASCAL A::_GetBaseClass()
{
return RUNTIME_CLASS(CObject); 
}



要了解更多信息,请参见msdn中的CRuntimeClass(),RUNTIME_CLASS等.



to understand more see what is CRuntimeClass(), RUNTIME_CLASS etc in msdn



这篇关于此宏IMPLEMENT_DYNCREATE的宏定义表示什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 16:47