Thursday, March 10, 2011


Makes you write code like this...
if ((ent = dynamic_cast(what)) && ent->GetGISClass() == gis_Composite) true;
At least C++ isn't judgmental.


  1. I take it you build without warnings, then.

    -wall can be very judgmental.

  2. -wall hurts my self esteem. :-)

  3. I'm a masochist so I build with these flags:
    -Wall -W -Wextra -Werror -Wcast-qual -Wc++0x-compat -std=c++0x
    and a lot more, [s][n][f]printf formatting, switch and for statement stuff, unused parameters, read only string literals etc.

  4. I always try to avoid dynamic_cast, can you replace it with an "IsA" function and static_cast? Or:
    bool IsGISCompositeClass() const { return (type == gis_Composite; }
    Or even:
    virtual bool IsGISCompositeClass() { return false; }

  5. Hi Chris,
    Re: dynamic cast...I need to examine the situation more carefully, but the class hierarchy has virtual base classes...I used some 'static' type checking (is my type X) to improve performance, because dynamic cast inside inner loops shows up on a perf profile. But..the whole project is sort of deeply in the dynamic type world. That'll have to be another blog post; it's not normally a style I like (and I am a big fan of static typing) but there were a lot of cases where independent subsystems had to be specialized in peculiar combinations to meet UI needs...

  6. You mean you use multiple inheritance from base classes ? That's generally a bad solution leading to lots of problems.

    Depending on the width of the class hierarchy it could be better to add an AsGISComposite() method to your base class(classes) returning a pointer to GisComposite where applicable, NULL otherwise, rather than IsGisCompositeClass(), which would doesn't get you rid of the dynamic_cast<>() in the case of multiple inheritance AND no pointer casting in the case of single inheritance.