writes:
> what is a reason for which RTTI is considered as a bad design?
> Stroustrup has written in his book TC++PL that the most common case of
> usage RTTI technique is with switch instruction, when one want to
> decide what code should be executed basing on what is a "real" type of
> a passed object. There is given an example in which an object of shape
> class was passed to the function and different actions were executed
> depending on that whether a shape is circle, square, triangle, etc. He
> has written that such construction is a sign that one should replace
> the switch-case sequence by virtual functions.
>
> In my opinion such dealing is reasonable if what should be done is
> really a matter of given type (for example counting the area of
> circle).
Using class/inheritance/virtual functions provides static information
(used by the compiler). RTTI is, well, run-time.
Suppose you add a new shape sub-class in your example. Using RTTI, the
compiler will be unable to tell you that you forgot to add the
corresponding case in your switch. If you use virtual functions, the
compiler will tell you that your new class fails to implement the
function.
> But is RTTI design undesirable in the situation in which a choice of
> action depends upon how an external module want to treat objects
> basing on types of them?
I can't parse your remark here. If you mean that an external module may
need dynamic type information on your objects to be able to handle them
properly, then why not provide the correct functions in your own
classes?
> For example if one is making a space-war game in which there are many
> kinds of ships usage of RTTI seems quite logical to me.
How can you be sure all kinds of ships are correctly handled by all of
your code? You can't. You have to manually inspect all switches.
-- Alain.