1# Example: C++ run-time type info (RTTI) 2 3(See the README.md file in the upper level 'examples' directory for more information about examples.) 4 5This example demonstrates usage of the RTTI feature in ESP-IDF. 6 7By default, RTTI support is disabled in ESP-IDF. It can be enabled using `CONFIG_COMPILER_CXX_RTTI` configuration option. 8 9In this example, `sdkconfig.defaults` file sets `CONFIG_COMPILER_CXX_RTTI` option. This enables compile time support for RTTI (`-frtti` compiler flag). 10 11The example prints demangled type names of a few objects and functions, obtained from `typeinfo().name`. The example also generates several objects of two classes, derived from a common base class. For each object, it is demonstrated that `dynamic_cast` behaves as expected, returning non-NULL when casting to the real object type, and NULL when casting to a different type. 12 13## How to use example 14 15### Configure the project 16 17To run this example, no additional configuration is necessary. 18 19### Build and Flash 20 21Build the project and flash it to the board, then run monitor tool to view serial output: 22 23``` 24idf.py -p PORT flash monitor 25``` 26 27(Replace PORT with the name of the serial port to use.) 28 29(To exit the serial monitor, type ``Ctrl-]``.) 30 31See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects. 32 33## Example Output 34 35``` 36Type names of a few objects: 37 Type name of std::cout is: std::ostream 38 Type name of std::cin is: std::istream 39 Type of app_main is: void () 40 Type name of a lambda function is: app_main::{lambda(int, int)#1} 41 42Generating 5 random objects and printing their types: 43obj->name() is: DerivedB 44 typeid(*obj).name() is: DerivedB 45 dynamic_cast<DerivedA*>(obj)=0 46 dynamic_cast<DerivedB*>(obj)=0x3ffb7e88 47 48obj->name() is: DerivedB 49 typeid(*obj).name() is: DerivedB 50 dynamic_cast<DerivedA*>(obj)=0 51 dynamic_cast<DerivedB*>(obj)=0x3ffb7e9c 52 53obj->name() is: DerivedA 54 typeid(*obj).name() is: DerivedA 55 dynamic_cast<DerivedA*>(obj)=0x3ffb7eb0 56 dynamic_cast<DerivedB*>(obj)=0 57 58obj->name() is: DerivedB 59 typeid(*obj).name() is: DerivedB 60 dynamic_cast<DerivedA*>(obj)=0 61 dynamic_cast<DerivedB*>(obj)=0x3ffb7ec4 62 63obj->name() is: DerivedA 64 typeid(*obj).name() is: DerivedA 65 dynamic_cast<DerivedA*>(obj)=0x3ffb7ed8 66 dynamic_cast<DerivedB*>(obj)=0 67 68Example finished. 69``` 70