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

Run-Time Type Information/RTTI

Run-Time Type Information is a property to identify the name or type of an object at runtime. C++ object usually do not contain type information. Run-Time Type Information is the name or type in the class. This helps to idetify the type as well as the hierarchil parent child relationship.

Run-Time Type Information is essential for client server applications where clients connect to server and passes object pointer to server. Server and clients are decoupled and developed seperately. Server should examine the objects provided by clients before processing them. Client can pass any type of object at runtime. Server sould examine Run-Time Type Information to check the type and the parent type if it is devived correctly. Object pointer contain a correct memory layout, size and vTable pointer when it is derived correctly otherwise server may crash while accessing members or calling virtual functions. Server should throw error to client if correct object is not provided. Hence Run-Time Type Information (RTTI) support s essential and introduced in MFC introduced in MFC 4.0.

RTTI macros

An MFC object should be able to hold RTTI information if the type class is derived from CObject and following of the macro pair is used in declaration and definition.

  1. DECLARE_DYNAMIC and IMPLEMENT_DYNAMIC,
  2. DECLARE_DYNCREATE and IMPLEMENT_DYNCREATE,
  3. DECLARE_SERIAL and IMPLEMENT_SERIAL

RTTI demo object validation

class CUnknownObj : public CObject
{
  DECLARE_DYNAMIC(CUnknownObj)
};
IMPLEMENT_DYNAMIC( CUnknownObj, CObject )

class CDrawObj : public CObject
{
  virtual void Draw(void) = 0;
  DECLARE_DYNAMIC(CDrawObj)
};
IMPLEMENT_DYNAMIC( CDrawObj, CObject )

class CRectangle : public CDrawObj
{
  virtual void Draw(void);
  DECLARE_DYNAMIC(CRectangle)
};
IMPLEMENT_DYNAMIC( CRectangle, CDrawObj )
void CRectangle::Draw(void)
{
  cout << "Draw Rect";
}

class CCircle : public CDrawObj
{
  virtual void Draw(void);
  DECLARE_DYNAMIC(CCircle)
};
IMPLEMENT_DYNAMIC( CCircle, CDrawObj )
void CCircle::Draw(void)
{
	cout << "Draw Circle";
}

/* Client side code */
CObject *GetObjectFromUser(char *ClassName)
{
	if(strcmp(ClassName, "CRectangle") == 0) 
	return new CRectangle();
	else  if(strcmp(ClassName, "CCircle") == 0)
	return new CCircle();
	else  if(strcmp(ClassName, "CUnknown") == 0)
	return new CUnknownObj;

}

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
	int nRetCode = 0;

	// initialize MFC and print and error on failure
	if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
	{
		// TODO: change error code to suit your needs
		cerr << _T("Fatal Error: MFC initialization failed") << endl;
		nRetCode = 1;
	}
	else
	{
		cout << "Server side object validation " << endl;

		/* Validate CRectangle object */
		CObject *obj = GetObjectFromUser("CRectangle");
		// TODO: code your application's behavior here.
		cout << "CRectangle is IsKindOf " << endl;
		if(obj->IsKindOf(RUNTIME_CLASS(CObject))!=0)
			cout <<"CObject " << endl;
		if(obj->IsKindOf(RUNTIME_CLASS(CDrawObj))!=0)
			cout <<"CDrawObj " << endl;
		if(obj->IsKindOf(RUNTIME_CLASS(CRectangle))!=0)
			cout <<"CRectangle " << endl;
		if(obj->IsKindOf(RUNTIME_CLASS(CCircle))!=0)
			cout <<"CCircle " << endl;

		cout << endl;

		/* Validate CCircle object */
		CObject *obj1 = GetObjectFromUser("CCircle");
		cout << "CCircle is IsKindOf "<< endl;
		if(obj1->IsKindOf(RUNTIME_CLASS(CObject))!=0)
			cout <<"CObject " << endl;
		if(obj1->IsKindOf(RUNTIME_CLASS(CDrawObj))!=0)
			cout <<"CDrawObj " << endl;
		if(obj1->IsKindOf(RUNTIME_CLASS(CRectangle))!=0)
			cout <<"CRectangle " << endl;
		if(obj1->IsKindOf(RUNTIME_CLASS(CCircle))!=0)
			cout <<"CCircle " << endl;

		cout << endl;

		/* Validate CUnknown object */
		CObject *obj2 = GetObjectFromUser("CUnknown");
		cout << "CUnknown is IsKindOf "<< endl;
		if(obj2->IsKindOf(RUNTIME_CLASS(CObject))!=0)
			cout <<"CObject " << endl;
		if(obj2->IsKindOf(RUNTIME_CLASS(CDrawObj))!=0)
			cout <<"CDrawObj " << endl;
		if(obj2->IsKindOf(RUNTIME_CLASS(CRectangle))!=0)
			cout <<"CRectangle " << endl;
		if(obj2->IsKindOf(RUNTIME_CLASS(CCircle))!=0)
			cout <<"CCircle " << endl;
	}

	return nRetCode;
}

Program output

Server side object validation
CRectangle is IsKindOf
CObject
CDrawObj
CRectangle

CCircle is IsKindOf
CObject
CDrawObj
CCircle

CUnknown is IsKindOf
CObject
Press any key to continue

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 Wed 30 Apr/2014 01:21:29(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.