Runtime Class Identification

Every class in the ObjectARX hierarchy that is derived from AcRxObject has a corresponding class descriptor object, which is an instance of AcRxClass that holds information for runtime type identification. The class descriptor object, gpDesc, is a static data member of the class—for example, AcDbEllipse::gpDesc. Class descriptor objects are created at initialization, when classes are registered with ObjectARX and are added to a system-level dictionary, acrxClassDictionary. The macros described here facilitate the declaration and implementation of certain functions related to runtime identification and initialization functions. These include the class initialization routine as well as the desc(), cast(),isKindOf(), and isA() functions for the custom class.

Important functions provided by the AcRxObject class for runtime type identification include the following:

When you want to know what class an object is, use AcRxObject::isA(). This function returns the class descriptor object (an instance of AcRxClass) for a database object. Its signature is

AcRxClass* isA() const;

When you already know what class an object is, you can use the desc() function to obtain the class descriptor object:

static AcRxClass* desc();

The following example looks for instances of AcDbEllipse or any class derived from it, using isKindOf() and the AcDbEllipse::desc() static member function:

AcDbEntity* curEntity = somehowGetAndOpenAnEntity();
if (curEntity->isKindOf(AcDbEllipse::desc())) {
   // Got some kind of AcDbEllipse instance.
}

This example shows another way of looking for instances of AcDbEllipse, or any class derived from it, using the AcDbEllipse::cast() static member function:

AcDbEllipse* ellipseEntity = AcDbEllipse::cast(curEntity);
if (ellipseEntity != NULL) {
   // Got some kind of AcDbEllipse instance.
}

The following example looks for instances of AcDbEllipse, but not instances of classes derived from AcDbEllipse, using isA() and AcDbEllipse::desc():

if (curEntity->isA() == AcDbEllipse::desc()) {
   // Got an AcDbEllipse, no more, no less.