Runtime class information using CRuntimeClass in MFC:

CoderSource.net
Runtime class information using CRuntimeClass in MFC:
Rating:

Date: 5/1/2004 12:00:00 AM

    C++ by itself does not have a feature for dynamic runtime class information. But Microsoft solved this problem by providing a CRuntimeClass.

    This structure can be used for dynamic runtime class information, extra type checking of function arguments or when we have to write some special purpose code based on the class of an object.

    This CRunitmeClass is a structure and does not have a base class. All the classes derived from the MFC root class CObject, are associated with CRuntimeClass structure. This CRuntimeClass provides runtime class information (such as pointer to the object, ASCII name of the class etc) about the associated classes. This structure also implements various functions that can be used to dynamically create objects, specifying the type of object by using a familiar name, and determining if the related class is derived from a specific class.

    To use this CRuntimeClass, the class for which the runtime information is to be deduced should use DECLARE_DYNAMIC and IMPLEMENT_DYNAMIC macros. Also the class must have been derived from CObject. There are two more important functions, which can be used in association with the CRuntimeClass. One function is IsKindOf and the other one is GetRuntimeClass.

IsKindOf:
    This IsKindOf function tests if an object is of a particular class type and returns TRUE or FALSE. This could be important runtime class information based on which certain business logic can be decided.

    The header file should use DECLARE_DYNAMIC macro inside the class for making it aware of CRuntimeClass structure.


//MyClass.h
    class MyClass :public CObject
    {
        DECLARE_DYNAMIC(MyClass)
       public:
       MyClass(){}
    };

    The source file (.cpp) should call IMPLEMENT_DYNAMIC(Derivedclass, rootclass) for using CRuntimeClass. This completes the runtime class information basic formalities. After this we can use IsKindof function or GetRuntimeClass for getting our much needed runtime class information.


//MyClass.cpp
    IMPLEMENT_DYNAMIC( MyClass, CObject )
    void MyFunction()
    {
       CObject *mObject = new MyClass;
       if(mObject->IsKindOf(RUNTIME_CLASS( MyClass) ) )
       {
          printf("Class is of type MyClass\n");
       }
       else
       {
          printf("Class is of type someotherclass\n");
       }
    }

GetRuntimeClass:
    This function returns a pointer to CRuntimeClass, which can be used to retrieve the name of the class.

    char strClassName[256];
    MyClass obj;
    CRuntimeClass *myClass ;
    myClass = obj.GetRuntimeClass();
    strcpy(strClassName,myClass->lpszClassName);


    It is to be noted that the use of CRunTimeClass for type checking at run time is against the rule of virtual functions on inheritance. But this can be used to write some special purpose code where some type checking is needed.

You Can Rate this Article, if you are Logged In      
 

More Links from CoderSource.net:

 
Refer to a Friend:

Your Details:

Name:     e-mail:

Friend Details:

Name:    e-mail:    


MENU
Home
MFC 
C++
.Net
WIN32
Programming
Forum
My Articles
Add to Google
Add to My Yahoo!
Welcome to Codersource.Net Login | Register | Faq  

SEARCH
Google
 

NOTES:


Thanks for visiting our CoderSource.net. This site will be improved with more articles. Interested visitors can also submit their articles through the Submit Article link.Your article will also be published after due consideration by the editor. 

© Copyright 2003. All rights on content reserved by CoderSource.net. Contact    About Us