Dynamic_cast on EmBitz

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.

1 Like

Remove fno_rtti compile flag

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.

2 Likes

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:

  1. Project
  2. Build Options
  3. Select name of your project in tree view (e.g. ‘HelloWorld’ or ‘Bitmap’)
  4. Select the Compiler settings tab
  5. Select the C++ - Flags tab
  6. Open the Categories: combo box
  7. Select C++ options
  8. 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.

2 Likes

Btw, there’s a reason for no RTTI.

Edit: yes, this is a part of the background story of Pokitto

1 Like

That’s how I fix it. Nice to know ho enable this on EmBitz, that’s quite buried in the deep indeed.

Love hear this stories.

Thanks, really fast responses.

1 Like

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.

6 posts were split to a new topic: Alternatives To dynamic_cast