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

CRuntimeClass Structure

CRuntimeClass is a structure to hold the runtime type, name, parent class and methods to indentify class information and dynamic creation function. MFC base class CObject holds CRuntimeClass and thus each class derived from CObject is associated with a CRuntimeClass structure that you can use to obtain information about an object or its base class at run time. Here are CRuntimeClass methods and member variables.

CRuntimeClass Methods

NameDescription
CreateObjectCreates an object during run time.
FromNameCreates an object during run time using the familiar class name.
IsDerivedFromDetermines if the class is derived from the specified class.

CRuntimeClass Members

m_lpszClassNameThe name of the class.
m_nObjectSizeThe size of the object in bytes.
m_pBaseClassA pointer to the CRuntimeClass structure of the base class.
m_pfnCreateObjectA pointer to the function that dynamically creates the object.
m_pfnGetBaseClassReturns the CRuntimeClass structure (only available when dynamically linked).
m_wSchemThe schema number of the class.

CreateObject/m_pfnCreateObject

class CMyClass : public CWnd
{
  DECLARE_DYNCREATE(CMyClass);
};
IMPLEMENT_DYNCREATE(CMyClass,CWnd)

int main() 
{
  CRuntimeClass* pClass = RUNTIME_CLASS(CMyClass);
  CObject* pObject = pClass->m_pfnCreateObject();
  or
  CObject* pObject = pClass->CreateObject();
}

CRuntimeClass::FromName

CRuntimeClass* pClass = CRuntimeClass::FromName(_T("CMyClass"));
if (pClass == NULL)
{
 // not found, display a warning for diagnostic purposes
 AfxMessageBox(_T("Warning: CMyClass not defined"));
 return NULL;
}

CRuntimeClass::IsDerivedFrom

// We only want to create an object derived from CWnd.
if (!pClass->IsDerivedFrom(RUNTIME_CLASS(CWnd)))
{
 TRACE(_T("Error; Object %s is not derived from CWnd\n"),
	pClass->m_lpszClassName);
 return FALSE;
}

ClassName, BaseClass, size, schema

// Get a pointer to the base class CRuntimeClass.
#ifdef _AFXDLL
 CRuntimeClass* pBaseClass = pClass->m_pfnGetBaseClass();
#else
 CRuntimeClass* pBaseClass = pClass->m_pBaseClass;
#endif
ASSERT(pBaseClass != NULL);

TRACE("Creating object %s derived from %s, with object size %d "
 "and schema %d\n", pClass->m_lpszClassName, 
 pBaseClass->m_lpszClassName, pClass->m_nObjectSize, 
 pClass->m_wSchema);

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 Tue 29 Apr/2014 04:43:32(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.