0

Having done with 1st Vol. of Thinking in C++ by Bruce Eckel, I have started reading the 2nd Vol. The chapter devoted to RTTI (Run-Time Type Identification) amazes me the most. I have been reading about tyepid, dynamic_cast, etc.

But, I have a question floating in my mind. Are their any practical uses of exploiting RTTI through the operators mentioned i.e. some examples from real-life projects? Also, what were the limitations encountered which made its use necessary?

Sankalp
  • 2,796
  • 3
  • 30
  • 43
  • Practical, yes. Common, no. Its use should be quite rare for most apps. I'd guess the biggest use of RTTI is to implement `boost::any`, which checks that the types you `get()` are the same that you assigned. – Cory Nelson Jul 08 '13 at 15:31
  • I've never seen nor used `typeid` anywhere serious, but `dynamic_cast` can be useful for *some* runtime polymorphism. That being said about `dynamic_cast`, if you need to use it it is often (but not always, of course) a sign of bad design. – Some programmer dude Jul 08 '13 at 15:34
  • [Answers here should probably answer your question](http://stackoverflow.com/questions/6751061/when-is-using-typeid-the-best-solution) – milleniumbug Jul 08 '13 at 18:33
  • Here is a sample of using `typeid`'s - http://stackoverflow.com/a/17549551/381333 – Konstantin Oznobihin Jul 09 '13 at 14:19

2 Answers2

1

dynamic_cast can be useful for adding optional functionality


void foo(ICoolStuff *cs)
{
  auto ecs = dynamic_cast<IEvenCoolerStuff*>(cs);
  if (ecs != 0)
  {
    ecs->DoEvenCoolerStuff();
  }

  cs->DoCoolStuff();
}

when you design from scratch it might be possible to put DoEvenCoolerStuff into ICoolStuff and have empty implementations in classes which don't support it, but it's often not feasible when you need to change existing code.

Another use is messaging system implementation where one might use dynamic_cast for distinguishing messages you are interested in. More generally speaking you might need it when faced with the expression problem.

Gabe
  • 84,912
  • 12
  • 139
  • 238
Konstantin Oznobihin
  • 5,234
  • 24
  • 31
0

The most common example of RTTI in production code that I have seen in my travels is dynamic_cast, but it is almost always used as a band-aid for a poor design.

dynamic_cast is useful primarily for polymorphic classes, and then for going from base to derived. But think about it. If you have a base pointer to a properly designed polymorphic class, why would you ever need a pointer to a derived type? You should, in theory, only ever need to call the virtual functions, and have the actual instantiation deal with the implementation details.

Now that being said there are cases where even though dynamic_cast is a band-aid, it is still the lesser of two evils. This is particulary true when "fixing" the broken design would imply a large maintenance project, and would have no performance implications. Suppose you have a 1 MLOC application, and fixing something that is academically broken would mean having to touch 100k lines of code. If there is no performance reason to make that change, then you are fixing it purely for the sake of fixing it, but you run the risk of creating dozens or hundreds of new bugs. It might not be worth it.

John Dibling
  • 99,718
  • 31
  • 186
  • 324
  • In theory there is no solution for expression problem in mainstream languages and you might need more than just virtual functions for good enough desing. Still one can say that dynamic_cast is a band-aid for a poorly designed type system. :) – Konstantin Oznobihin Jul 08 '13 at 16:16
  • I didn't really understand that. – John Dibling Jul 08 '13 at 16:23
  • 3
    @John Dibling: Frankly, I heard this "bad design" argument many times. But at the same time I haven't seen any good solutions for typical everyday problems, like, for example, when I place a `Derived` object in a polymorphic container (which stores, say, `Base *` pointers). Now I want my get my `Derived` object back as `Derived`. How can I do that without a cast? It is actually rhetorical question. OOP in C++ without casts is only possible for simplistic laboratory-grade examples. Any serious practical programming will require casts and there's no way around it. – AnT stands with Russia Jul 08 '13 at 16:35
  • @JohnDibling +1 for `dynamic_cast`, but any specific comments on `typeid`? – Sankalp Jul 08 '13 at 16:39
  • Dynamic casts are necessary for operations that depend on the type of an object, when those operations don't belong as virtual members of the base class. That is, when the behavior of an object depends on what class it is, you simply add a virtual method. When the behavior of a 3rd-party object depends on what class something is, that 3rd-party class should use a dynamic cast. – Gabe Jul 08 '13 at 16:45
  • @JohnDibling: try googling for 'expression problem' – Konstantin Oznobihin Jul 08 '13 at 17:11
  • @AndreyT: The question is, why would you need to get it back as `Derived`? Perhaps this is also a rhetorical question, as I agree that `dynamic_cast` does and should come up in real software. Ever has it been thus, and ever shall it be. – John Dibling Jul 08 '13 at 17:54
  • @JohnDibling: e.g. because you can have either run-time polymorphism and you have to deal with IBaseObject collections or you can have compile-time polymorphism and deal with vector, but you can't have both. – Konstantin Oznobihin Jul 08 '13 at 18:17