YOU CAN CODE!

 

With The Case Of UCanCode.net  Release The Power OF  Visual C++ !   HomeProducts | PurchaseSupport | Downloads  
XD++ Library
DocVizor
TFC Library
Free Products
Technical Support
UCanCode.net


Get Ready to Unleash the Power of UCanCode .NET


UCanCode Software focuses on general application software development. We provide complete solution for developers. No matter you want to develop a simple database workflow application, or an large flow/diagram based system, our product will provide a complete solution for you. Our product had been used by hundreds of top companies around the world!

"100% source code provided! Free you from not daring to use components because of unable to master the key technology of components!"


Runtime class information using CRuntimeClass in MFC:

 
 
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.

 

 

Copyright ?1998-2007 UCanCode.Net Software , all rights reserved.
Other product and company names herein may be the trademarks of their respective owners.

Please direct your questions or comments to webmaster@ucancode.net