Click here to Skip to main content
15,880,503 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
What is the difference between dynamic memory allocation (new&delete) and dynamic creation(CObject derived class)? DMA tells that memory is created on heap dynamically for the objects. DECLARE_DYNCREATE creates the objects dynamically...so why do we use this macro? Aren't they similar? If not, why?
Posted

Dynamic creation is the ability to create the object of a class at runtime and for doing this effectively we need to access the class's (and it's base) type information.
By deriving a class from CObject (and putting the necessary macros) we get this information via CRuntimeClass structure.

Now dynamic memory is the process of allocation memory on the heap rather than on the stack.
 
Share this answer
 
Comments
stib_markc 12-Apr-12 2:34am    
thank you. but what do you mean by "doing this effectively"? I mean we use RTTI for dynamic-casting, generally. Is RTTI that much essential / does it provide many advantages?
Lakamraju Raghuram 12-Apr-12 2:41am    
We can access type information of a class by RTTI and for doing this I guess we should trun on a compiler switch (if I am correct) and also we should have atleast one virtual function (the whole idea is to inject vptr into the class object). Now this is all done by putting those macors.

Well again the usability of type information at run-time is case driven
stib_markc 12-Apr-12 8:00am    
thank you. I found some code using RTTI for audi programming, what i cannot figure out is why he used it. I will try to write my own code, instead of trying to understand complex code.
By the implemented macro -
you can use the default constructors of an objects chain
without to know something about the chain's level :) :
C++
// class CShape : public CObject
// class CCircle : public CShape
// class CRectangle : public CShape

CShape* CYourShapeTool::MakeCopy(CShape* pcSrcShape)
{
  ASSERT(pcSrcShape);

  CShape* pcResShape = STATIC_DOWNCAST(
    CShape, pcSrcShape->GetRuntimeClass()->CreateObject() // Default constuctor's call only
  );
  ASSERT(pcResShape);
  
  // Now you could call the virtual void CShape::Assign(const CShape* pcSrc),
  // overwritten by each class (CShape, CCircle, CRectangle) -
  // to copy the properties (class members) as well:
  pcResShape->Assign(pcSrcShape);
  
  return pcResShape;
}

void TestFunction()
{
  CYourShapeTool cTool;

  CCircle cCircle(10, 10, 5); // Non-default constructor's call
  CCircle* pcCopyCircle = STATIC_DOWNCAST(
    CCircle, cTool.MakeCopy(&cCircle) // Default constructor + Assign
  );
  delete pcCopyCircle;
  pcCopyCircle = NULL;

  CRectangle cRect(20, 20, 50, 50); // Non-default constructor's call
  CRectangle* pcCopyRect = STATIC_DOWNCAST(
    CRectangle, cTool.MakeCopy(&cRect) // Default constructor + Assign
  );
  delete pcCopyRect;
  pcCopyRect = NULL;
}
 
Share this answer
 
v3
Comments
stib_markc 12-Apr-12 2:31am    
thank you
DECLARE_DYNCREATE is MFC stuff, This happens in several scenarios, MFC uses CreateObject function to create the corresponding class. Try creating a SDI application using wizard and see the CYourApp::InitInstance function, debug it.. you will understand.. :)
 
Share this answer
 
Comments
stib_markc 12-Apr-12 2:31am    
Thank you

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