In C++, in Object Oriented Programming what is RTTI?
- User
In C++, in Object Oriented Programming what is RTTI?
- Assistant
RTTI stands for Run-Time Type Information in C++. It is a mechanism that enables the type of an object to be determined during program execution. RTTI is primarily used for identifying the real type of an object when you have a pointer or a reference to a base class. This feature can be useful when working with polymorphic class hierarchies.
The key components of RTTI in C++ are:
-
typeid Operator: This operator is used to determine the exact type of an object. It returns a reference to a
type_infoobject, which contains information about the type. For example, can be used to compare the types of two objects.
-
- User
Consider the following snippet: ~ class Base { public: virtual ~Base(); };
class Derived : public Base { };
int main() { Base* myClass = new Derived(); delete myClass; return 0; } ~ In assembler, it creates this part related to RTTI: ~ typeinfo for Derived: .quad vtable for __cxxabiv1::__si_class_type_info+16 .quad typeinfo name for Derived .quad typeinfo for Base typeinfo name for Derived: .string "7Derived" ~ , that should be related to RTTI. I know what I can add the flag -fno-rtti, that should delete this part. However, it is not clear which are the consequences of adding this flag. Please provide feedback on it.
- Assistant
Using the
-fno-rttiflag in C++ has specific consequences that stem from disabling Run-Time Type Information (RTTI). Here’s a detailed explanation of the implications:-
Disabling Type Identification: With , you won't be able to use the operator. Attempting to use it will result in a compilation error. This means you cannot determine the exact type of an object during runtime.
-