Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1675974
  • 博文数量: 585
  • 博客积分: 14610
  • 博客等级: 上将
  • 技术积分: 7402
  • 用 户 组: 普通用户
  • 注册时间: 2008-05-15 10:52
文章存档

2013年(5)

2012年(214)

2011年(56)

2010年(66)

2009年(44)

2008年(200)

分类: C/C++

2009-07-25 22:03:35

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. ThisCRuntimeClass 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 isGetRuntimeClass.

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);
阅读(1289) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~