• (nodebb)

    It's almost like they tried to invent reflection. You can query properties of the class to dynamically learn about the class.

    Serious brain damage there.

    The fact the OP tells us this idiom is used inconsistently everywhere suggests one "genius" had invented this at some prior job, brought it here, proselytized it to the unwashed mass of ordinary devs, most of whom never understood it well enough to use it right (or at least as rightly as such insanity could be used), then moved on to the next company & next project. Or was promoted to some executive manager of dev roles here and so was able to insist it be used, but not pay attention to how it was used.

  • (nodebb)

    Now, I know that they are running with RTTI enabled, and thus when they do a bad cast, dynamic_cast will throw a bad_cast exception? How do I know?

    Observation: An incorrect dynamic_cast on pointers returns NULL instead of throwing bad_cast.

  • (author) in reply to Steve_The_Cynic

    According to S5.iii it can indeed throw a bad cast.

  • (nodebb) in reply to Remy Porter

    According to S5.iii it can indeed throw a bad cast.

    You need to read that section more carefully. Specifically section 5.b.iii (there is no section 5.iii), which covers the error case.

    iii) Otherwise, the runtime check fails.

    . If target-type is a pointer type, the result is the null pointer value of target-type. <<== Read this line.

    . If target-type is a reference type, an exception of a type that would match a handler of type std::bad_cast is thrown.

    Addendum 2026-02-02 09:22: Above all, please note the case where the input and output are pointers.

  • (nodebb)

    This seems like someone had implemented OO in C, then migrated it to C++.

  • (nodebb)

    There are various reasons for not posting a reply, but the best one is that you have nothing to say because the WTF is too horrible to contemplate.

  • (nodebb)

    Somewhere there is an unused virtual function table quietly weeping.

  • Me (unregistered)

    To me this looks more like a badly executed reinvention of tagged unions.

  • (nodebb) in reply to Me

    I misread that as tagged onions at first. Tagged onions seem like they might be more useful than the featured code.

  • (author) in reply to Steve_The_Cynic

    This is a case where I should have read your post more closely, since yes, on pointers you're correct, but the code in question doesn't always do it on pointers: dynamic_cast<Derived_1&>(this);

  • (nodebb) in reply to davethepirate

    Somewhere there is an unused virtual function table quietly weeping.

    Only if they declared any virtual functions. RTTI has to be able to function even when there aren't virtual functions (yeah, I know, setting up a class which gets inherited from, but doesn't have at least a virtual destructor, is TRWTF), and there aren't any requirements that the vtable exists (even when there are virtual functions)(1), much less that it contains a pointer to the RTTI information.

    (1) Yes, that's right. C++ virtual function dispatch was, in the way-back-when of the early nineties, critiqued for being hideously slow, but that was with ancient compilers that did a different kind of lookup that has long since been completely abandoned, and was much slower than "look in the object for the vtable base, look in there for the function pointer for vfunction 7, call it".

  • (nodebb) in reply to Remy Porter

    Fair enough, although I did originally say that I was talking about the case of pointers.

    An incorrect dynamic_cast on pointers returns NULL

    And it might have been a good idea to mention the "references" version of the functions in the article text.

  • (nodebb) in reply to Me

    This is exactly right. I'd love to tear all of this out and replace it with a std::variant, but the code is still on c++11. My soul; it burns.

  • (nodebb) in reply to Steve_The_Cynic

    Only if they declared any virtual functions.

    There are. In fact, the first "Base" class has five pure virtual functions.

  • (nodebb) in reply to thaines.astro

    OK, but on the evidence given in the article, I can't conclude that. (And the whole situation suggests that "no virtual functions here" is an entirely reasonable assumption.)

Leave a comment on “Wages of Inheritance”

Log In or post as a guest

Replying to comment #690841:

« Return to Article