__rtti, -RT Option

From Appmethod Topics
Jump to: navigation, search

Go Up to Keywords, Alphabetical Listing Index

Category of __rtti keyword

Modifiers, Keyword Extensions, C++ Specific Keywords


Run-time type identification is enabled by default. You can disable RTTI on the C++ page of the Project Options dialog box. From the command line, you can use the -RT- option to disable it or -RT to enable it.

If RTTI is disabled, or if the argument to typeid is a pointer or a reference to a non-polymorphic class, typeid returns a reference to a const type_info object that describes the declared type of the pointer or reference, and not the actual object that the pointer or reference is bound to.

In addition, even when RTTI is disabled, you can force all instances of a particular class and all classes derived from that class to provide polymorphic run-time type identification (where appropriate) by using the __rtti keyword in the class definition.

When run-time type identification is disabled, if any base class is declared __rtti, then all polymorphic base classes must also be declared __rtti.

struct __rtti S1 { virtual s1func(); };  /* Polymorphic */
struct __rtti S2 { virtual s2func(); };  /* Polymorphic */
struct X : S1, S2 { };

If you turn off the RTTI mechanism, type information might not be available for derived classes. When a class is derived from multiple classes, the order and type of base classes determines whether or not the class inherits the RTTI capability.

When you have polymorphic and non-polymorphic classes, the order of inheritance is important. If you compile the following declarations without RTTI, you should declare X with the __rtti modifier. Otherwise, switching the order of the base classes for the class X results in the compile-time error "Can't inherit non-RTTI class from RTTI base 'S1'."

struct __rtti S1 { virtual func(); };   /* Polymorphic class */
struct S2 { };                           /*  Non-polymorphic class */
struct __rtti X : S1, S2 { };

Note: The class X is explicitly declared with __rtti. This makes it safe to mix the order and type of classes.

In the following example, class X inherits only non-polymorphic classes. Class X does not need to be declared __rtti.

struct __rtti S1 {  };   // Non-polymorphic class
struct S2 { };
struct X : S2, S1 { };   // The order is not essential

Neither the __rtti keyword, nor enabling RTTI will make a static class into a polymorphic class.

See Also