EQuestionAnswers.com Computer/Electronics Questions and Answers
C, C++, VC++, COM/DCOM, DLL and more
#Login #Sign up  Facebook Twitter TGoogle+
 

#Previous question #Index of VC++/WIN32/MFC Questions #Next question

Frame window

MFC frame base aplication or dialog base application creates a frame window by directly with new operator. Main window is assigned to a base i.e. class CWnd pointer and framework takes care rest of the things.

virtual BOOL InitInstance()
{
  m_pMainWnd = new CMyWnd();
  m_pMainWnd->ShowWindow(SW_SHOW);
  m_pMainWnd->UpdateWindow();
  return TRUE;
}

DOC/VIEW objects

Doc view application never creates a DOC, VIEW and FRAME object directly with new operator. Here framework takes runtime pointer RUNTIME_CLASS.

	CSingleDocTemplate* pDocTemplate;
	pDocTemplate = new CSingleDocTemplate(
		IDR_MAINFRAME,
		RUNTIME_CLASS(CMySDIDoc),
		RUNTIME_CLASS(CMainFrame),
		RUNTIME_CLASS(CMySDIView));
	AddDocTemplate(pDocTemplate);

CSingleDocTemplate constructor

Runtime class pointers are validated in runtime and saved in member variables in CDocTemplate.

	CDocTemplate::CDocTemplate(UINT nIDResource, CRuntimeClass* pDocClass,
	CRuntimeClass* pFrameClass, CRuntimeClass* pViewClass)
{
	ASSERT_VALID_IDR(nIDResource);
	ASSERT(pDocClass == NULL ||
		pDocClass->IsDerivedFrom(RUNTIME_CLASS(CDocument)));
	ASSERT(pFrameClass == NULL ||
		pFrameClass->IsDerivedFrom(RUNTIME_CLASS(CFrameWnd)));
	ASSERT(pViewClass == NULL ||
		pViewClass->IsDerivedFrom(RUNTIME_CLASS(CView)));
	m_pDocClass = pDocClass;
	m_pFrameClass = pFrameClass;
	m_pViewClass = pViewClass;

}

CreateNewDocument call stack

Framework calls CreateNewDocument at New document event.

CMySDIDoc::CMySDIDoc()
CMySDIDoc::CreateObject()
CRuntimeClass::CreateObject()
CDocTemplate::CreateNewDocument()
CSingleDocTemplate::OpenDocumentFile()
CDocManager::OnFileNew()

CDocTemplate::CreateNewDocument

RUNTIME class is casted to base pointer i.e. CDocument and CreateObject() is called. CreateObject() function calls m_pfnCreateObject function pointer.


CDocument* CDocTemplate::CreateNewDocument()
{
	// default implementation constructs one from CRuntimeClass
	if (m_pDocClass == NULL)
	{
		TRACE0("Error: you must override CDocTemplate::CreateNewDocument.\n");
		ASSERT(FALSE);
		return NULL;
	}
	CDocument* pDocument = (CDocument*)m_pDocClass->CreateObject();
	
}

CRuntimeClass::CreateObject

CRuntimeClass uses base abstruct class CObject and calls function pointer which eventually calls the derived class CreateObject.

CObject* CRuntimeClass::CreateObject()
{
	if (m_pfnCreateObject == NULL)
	{
		TRACE(_T("Error: Trying to create object which is not ")
			  _T("DECLARE_DYNCREATE \nor DECLARE_SERIAL: %hs.\n"),
			m_lpszClassName);
		return NULL;
	}

	CObject* pObject = NULL;
	TRY
	{
		pObject = (*m_pfnCreateObject)();
	}
	END_TRY

	return pObject;
}

CreateObject and new call

Derived class implements this CreateObject() and calls new operator. CMySDIDoc defines our CreateObject() with IMPLEMENT_DYNCREATE() macro and thus new operator calls the constructor CMySDIDoc().

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

Thus creation of DOC, View and Frame are all at runtime with dynamic function pointer. This RUNTIME class and DECLARE_DYNCREATE & IMPLEMENT_DYNCREATE makes all things.

You have viewed 1 page out of 109. Your VC++ learning is 0.00% complete. Login to check your learning progress.

 Vote 0

Similar topics related to this section

#Programming Windows, Fifth Edition (Developer Reference)
#Programming Applications for Microsoft Windows (Microsoft Programming Series)
#Programming Windows with MFC, Second Edition
#Visual C++ Programming
#Programming Microsoft Visual C++, Fifth Edition

* #1 webmaster Sun 27 Apr/2014 16:04:57(GMT)  Like 0 Unlike 0

Dear Users,

We are pleased to inform you that a forum/blog has been incorporated with www.mybestnotes.co.in. You are welcomed to add your comments, requests, codes, solutions and feedback to it. Please login(if already a member) or signup(for free) to avail to this facility.

Regards,
Webmaster

Your message goes here:

Name:Guest
Email:anonymous@unknown.com
My Post:*
Secutiry Code: ******  *
Preview this compose before posting this in discussion forum.
 
Note:
  1. Use [Sxx] code to show smiles. Example [S02]
  2. Use [URL ], example [URL http://www.google.com/]
  3. To display code or un formatted text use [CODE] [/CODE]. Example: [CODE] printf("Hello world"); [/CODE]
 [S01]   [S02]   [S03]   [S04]   [S05]   [S06]   [S07]   [S08] 
 [S09]   [S10]   [S11]   [S12]   [S13]   [S14]   [S15]   [S16] 
 [S17]   [S18]   [S19]   [S20]   [S21]   [S22]   [S23]   [S24] 

Note: Only members are allowed, * fields are mandatory.