Does anybody use dynamic_cast<MyClass *> in EmBitz ?
It reports:
'dynamic_cast' not permitted with -fno-rtti
I have a vectory of baseClass pointer that contains all the object instance in my game. Any instance is a derived clas of this base class. During an iteration I have to check the type so I try to cast the object with:
for(GameObj* e:entities)
{
if(dynamic_cast<Plane*>(e))
{
//Here should come only Plane istances...
}
}
This compile in CodeBlocks but not in Embitz
Iâve found a workaround but it would be nice to investigate.
fno-rtti
Disable generation of information about every class with virtual functions for use by the C++ runtime type identification features (dynamic_cast' andtypeidâ). If you donât use those parts of the language, you can save some space by using this flag. Note that exception handling uses the same information, but it will generate it as needed. The `dynamic_castâ operator can still be used for casts that do not require runtime type information, i.e. casts to void * or to unambiguous base classes.
The error 'dynamic_cast' not permitted with -fno-rtti means that you canât use a dynamic_cast because the compiler has been told to not emit run-time type information (RTTI).
dynamic_cast requires RTTI because of the way it works - dynamic_cast checks that the type is correct at runtime before trying to cast, and returns a nullptr if the type cannot be cast.
To fix this you need to remove the -fno-rtti compiler flag, which like most compiler flags can be accessed from the project build options:
Project
Build Options
Select name of your project in tree view (e.g. âHelloWorldâ or âBitmapâ)
Select the Compiler settings tab
Select the C++ - Flags tab
Open the Categories: combo box
Select C++ options
Untick "Donât generate runtime type identification (RTTI)`
By the way, generally dynamic_cast is avoided in favour of making use of virtual functions. If you can think of a way to achieve what you want to do by overriding virtual functions then prefer that way because it doesnât require RTTI and itâs generally considered to be more flexible. Sometimes dynamic_cast canât be avoided, but usually thereâs an alternative.
Thatâs good.
Switching to using virtual is actually a solution, not a workaround.
If anything, dynamic_cast is the workaround.
Theyâre âhidden awayâ for two reasons.
Firstly because compiler flags are a somewhat advanced topic - understanding the flags requires knowledge of the language, the compilation process and the specific compiler being used.
And secondly because they usually only have to be edited once or twice, itâs probably not something youâd need to be accessing regularly.
That said, I would argue that theyâre not entirely âburiedâ.
To someone who knows about compiler flags, the build options are actually the logical place to look.
Though I wonât argue that build options menus never look particularly nice. Thereâs so many options that itâs hard to make an interface that isnât cluttered up with tabs, combo boxes and check boxes everywhere.
Huh, I was expecting it was because RTTI increases code size and comes with a performance code.
I presume âEm::Blocksâ was the old name for EmBitz.