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


DECLARE_DYNAMIC and IMPLEMENT_DYNAMIC are used in conjunction to give runtime type identification ability to a class object.


This macro adds the ability to access run-time information about an object's class when deriving a class from CObject.
Syntax: DECLARE_DYNAMIC(class_name )
Add the DECLARE_DYNAMIC macro to the header (.h) module for the class, then include that module in all .cpp modules that need access to objects of this class. If DECLARE_DYNAMIC is included in the class declaration, then IMPLEMENT_DYNAMIC must be included in the class implementation.


Generates the C++ code necessary for a dynamic CObject-derived class with run-time access to the class name and position within the hierarchy.
Syntax: IMPLEMENT_DYNAMIC(class_name, base_class_name )
Remarks Use the IMPLEMENT_DYNAMIC macro in a .cpp module, and then link the resulting object code only once.

Demo App

RUNTIME_CLASS macro and the CObject::IsKindOf function is used to determine the parent class name of an objects at run time. For example we have a drawing application and this uses an abstruct class interface called CDrawObj to draw any shape. We have given user to add custom shapes through library. This is to extend the drawing facility. User should derive a custom class from CDrawObj and should define Draw() function. User can pass any object pointer of any type and application might crash while calling Draw() directly. Thus application should validate the object if it is derived from CDrawObj and then it should invoke Draw(). In this application we are checking user provided object if these are kind of CDrawObj.

#include <afx.h>
CWinApp theApp;
using namespace std;
class CDrawObj : public CObject
    virtual void Draw(void) = 0;
10  };
11  IMPLEMENT_DYNAMIC( CDrawObj, CObject )
13  /* This is user side implementation in library */
14  class CRectangle : public CDrawObj
15  {
16      virtual void Draw(void);
17    DECLARE_DYNAMIC(CRectangle)
18  };
19  IMPLEMENT_DYNAMIC( CRectangle, CDrawObj )
20  void CRectangle::Draw(void)
21  {
22    cout << "Draw Rect";
23  }
24  class CCircle : public CDrawObj
25  {
26      virtual void Draw(void);
28  };
29  IMPLEMENT_DYNAMIC( CCircle, CDrawObj )
30  void CCircle::Draw(void)
31  {
32    cout << "Draw Circle";
33  }
34  CObject *GetObjectFromUser(char *ClassName)
35  {
36    if(strcmp(ClassName, "CRectangle") == 0) 
37    return new CRectangle();
38    else  if(strcmp(ClassName, "CCircle") == 0)
39    return new CCircle();
40  }
41  /* Above section is in user side library */
43  int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
44  {
45    /* initialize MFC and print and error on failure */
46    if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
47    {
48    cerr << _T("Fatal Error: MFC initialization failed") << endl;
49    return 1;
50    }
51    CObject *obj = GetObjectFromUser("CRectangle");
52    cout << "CRectangle is IsKindOf CDrawObj " << obj->IsKindOf(RUNTIME_CLASS(CDrawObj)) << endl;
53    cout << "CRectangle is IsKindOf CObject " << obj->IsKindOf(RUNTIME_CLASS(CObject)) << endl;    
55    CObject *obj1 = GetObjectFromUser("CCircle");
56    cout << "CRectangle is IsKindOf CDrawObj " << obj1->IsKindOf(RUNTIME_CLASS(CDrawObj)) << endl;
57    cout << "CRectangle is IsKindOf CObject " << obj1->IsKindOf(RUNTIME_CLASS(CObject)) << endl;
59    return 1;
60  }


CRectangle is IsKindOf CDrawObj 1
CRectangle is IsKindOf CObject 1
CRectangle is IsKindOf CDrawObj 1
CRectangle is IsKindOf CObject 1

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 Mon 28 Apr/2014 19:28:03(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.


Your message goes here:

My Post:*
Secutiry Code: ******  *
Preview this compose before posting this in discussion forum.
  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.