Re: Misuses of RTTI
ouferrat@gmail.com (OuFeRRaT) wrote (abridged):
I don't understand the sentence:
"but the usual rule concerning RTTI is more or less the same as with
goto statements" can anyone explain it?
Goto is a low-level control, and most possible applications of it are
better represented with higher level constructs such as for, while,
switch etc. Writing shorter functions helps.
The author is saying that RTTI (presumably meaning typeof and
dynamic_cast<>) is also low-level, and most possible applications of it
are better represented with higher level constructions such as virtual
functions, interfaces/multiple inheritance, the Visitor type-discovery
pattern, and so forth. Sometimes we can use templates or other mechanisms
to avoid losing track of the exact type in the first place, making RTTI
redundant.
I broadly agree. However, one difference is that goto is a local
construct, within a single function, and we can rewrite the function to
not use it without affecting anything else. Where-as getting rid of RTTI
often means revising the architecture across many different classes.
Getting it right first time often requires careful design and foresight.
If some of the code is not under our control, then the ideal design may
not be possible, and even if all the code is under our control, an
RTTI-less design may mean more coupling, or rather more lack of locality,
than we want. Eg we may not want to inflict the burden of supporting a
particular virtual function, or the Visitor pattern, on every class in a
hierarchy.
So this local versus non-local issue makes it not a perfect analogy, and
in practice I find I use RTTI far more often than goto. However, the
author's point remains. Goto and RTTI are not the tools to keep at the
very top of your toolbox. Try the alternatives first.
-- Dave Harris, Nottingham, UK.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]