Dynamic creation of Object

MFC/C++ uses new operator to create an object from a constant type at runtime. MFC supports dynamic creation of object using type with runtime type identification support. DYNCREATE macro pair is used for this purpose.


This macro enables objects of CObject-derived classes to be created dynamically at run time. Ths is used in the declaration part of the class and thus can be added in .h file.


/* class_name = The actual name of the class.*/


Enables objects of CObject-derived classes to be created dynamically at run time when used with the DECLARE_DYNCREATE macro. Ths is used in the implementation part of the class and thus can be added in .h file.


IMPLEMENT_DYNCREATE(class_name, base_class_name )
/* class_name = The actual name of the class.
   base_class_name = Base class name.


class CMyDynCreateObj : public CObject
     int m_Num;
     CMyDynCreateObj(int Num) { m_Num = Num; }
     CMyDynCreateObj() { m_Num = 0; } 


void TestCreateObject(void)
  CRuntimeClass* prt = RUNTIME_CLASS(CAge);
  ASSERT(strcmp(prt->m_lpszClassName, "CAge") == 0);

About our authors: Team EQA

Further readings

Where is WinMain() function in MFC application ?

MFC hides WinMain in its framework and includes source file on WinMain(). This explains how framework calls global CWinApp::Initinstance() from entry WinMain.

What is the utility of CWinApp class?

This is constructed during global C++ objects are constructed and is already available when Windows calls the WinMain function, which is supplied by the ...

Basic steps in Win32 GUI Application with source code.

Define a custom Window class structure, Register the class name, CreateWindow, Show windows and write message get and dispatch loop statements. Define the Window CallBack procedure and write the handlers.

What is a Window CallBack procedure and what is its utility?

DispatchMessage() is a API which indirectly triggers the Window CallBack procedure. Message structure members from this function are passed to the CallBack procedure. CallBack procedure should implement event handlers depending on the need of the application.

What are LPARAM and WPARAM in window proc function?

LPARAM and WPARAM are the two parameters in Window CallBack procedure. They signifies parameters of various events. They are used in handing individual events.

What are the basic steps of a typical MFC based application?

We need to write WinMain and need to follow all these in a Win32 application. However we need not to write much if we are writing an application with MFC ...