Click here to Skip to main content
15,921,028 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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)
Posted
Updated 31-May-11 20:46pm
v2

Hope you know about MFC dynamic object creation and how to use it.
I assume you just want to know what actually happens when we write IMPLEMENT_DYNCREATE.
Normally we code to use dynamic object creation facility as below
// 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); 
}


to understand more see what is CRuntimeClass(), RUNTIME_CLASS etc in msdn
 
Share this answer
 
:OMG:

Follow this link[^] and you'll get it in at least the first 3 of them.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900