next up previous index
Next: Additional RTTI Tools Up: RTTI Support for Previous: RTTI Basic Tools

Adding RTTI to a C++ Class

 

This section describes the steps to be taken to add RTTI capability to a C++ application class. These consist of two main operations. The first one amounts to adding one line to the class declaration. For the following example of a declaration for a C++ class called C:

        class C {
                   .....

                };

We only need to add the text TYPE_DATA to the declaration. This is best done by adding it right before the closing brace (although it is legal to add it anywhere in the class declaration):

        class C {
                   .....

                   TYPE_DATA
                };

The second operation consists of adding a macro to the file containing the class definition (i.e. the .cc file). The macro has the general form:

RTTI_DEFn(classname,"classname",,..., )

Here classname is the C++ class we add RTTI to, n is the number of direct base classes of classname which have RTTI (omit n if classname has no bases with RTTI), "classname" is the textual name we want to have in the RTTI system for the C++ class classname and ,..., are the C++ direct bases of classname which have RTTI.

For example: for a class declared as follows:

        class C : public A , public B

We should add the following line to its definition file:

        RTTI_DEF2(C,"C",A,B)

A last remark: if we plan to use the run-time object creation feature of the system, we have to declare which RTTI-classes are instantiable by a default constructor and which not. In order to make a C++ class run-time instantiable, one should use exactly the same procedure as above but replace the macro name RTTI_DEF byRTTI_DEF_INST. Of course, that class should have a public default constructor, otherwise it is impossible firstly to create an instance of it and secondly not to supply any constructor parameters to the TYPE_NEW macro previously described.



Alexandru Telea