next up previous index
Next: Implementation of Dynamic Up: Automatic Creation of Previous: The TYPE_DATA Macro

The RTTI_DEF and RTTI_DEF_INST Macros

The RTTI_DEF and RTTI_DEF_INST expand into the definition of the class members declared in each application class by the TYPE_DATA macro. The sematics of the methods having been presented in the previous sections, we shall only describe the initialization of the Type_info member.

The Type_info member contains the following members to be initialized: the type's textual name, the Type_infos for the type's direct bases and for the type's direct subclasses and a pointer to this type's static method. All this information can be easily obtained since the RTTI_DEF macros provide it directly or indirectly via their parameters.

One case deserves however particular care: the initialization of the list of direct subclasses of a Type_info. This list is implemented as a Type_info** and is called subtypes. In a project having multiple files with multiple classes, it may happen that a Type_info for a superclass tries to add itself to the subclass member of one of its subclass Type_infos which has not been constructed yet. We can not help this situation since the order of construction of static class members in a C++ program is undefined. One might think that this situation leads to unpredictable or dangerous behaviour. This is however not the case, since the compiler first allocates all static objects and initializes them to zero and only then starts calling their constructors. Since Type_info calls no virtual methods in its constructor and since we're guaranteed that all Type_info static objects are zeroized at the time the first Type_info constructor is called, we can guarantee that our initialization scheme (which relies on finding zeroized Type_infos and calls non-virtual methods on not yet constructed static Type_infos) is safe for any compilation system.



Alexandru Telea