home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / cplus / 16126 < prev    next >
Encoding:
Text File  |  1992-11-11  |  4.1 KB  |  77 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!ascent!eb
  3. From: eb@ascent.com (Ed Barton)
  4. Subject: Re: member test?
  5. In-reply-to: sakkinen@jyu.fi's message of Tue, 10 Nov 1992 07:01:24 GMT
  6. Message-ID: <EB.92Nov11123053@ascent.ascent.com>
  7. Date: 11 Nov 92 12:30:53
  8. References: <1992Nov6.081658.29693@bernina.ethz.ch> <EB.92Nov9085335@ascent.ascent.com>
  9.     <1992Nov10.070124.5763@jyu.fi>
  10. Organization: Ascent Technology, Inc., Cambridge Massachusetts
  11. Lines: 64
  12.  
  13.  
  14. In article <1992Nov10.070124.5763@jyu.fi> sakkinen@jyu.fi (Markku Sakkinen) writes:
  15.    In article <EB.92Nov9085335@ascent.ascent.com> eb@ascent.com (Ed Barton) writes:
  16.    >In article <1992Nov6.081658.29693@bernina.ethz.ch> haechler@bernina.ethz.ch (Stefan Haechler) writes:
  17.    >   How can I know which dynamic type has a variable ?
  18.    > ...
  19.    >The software-design answer is that you shouldn't have to know the
  20.    >answer to this question, and that there may be something wrong with
  21.    >the design if you need to ask it.  If you know only that something is
  22.    >a pointer-to-A, you should be using only the methods and virtual
  23.    >functions that are part of the interface defined by class A.
  24.    > ...
  25.    Come on, there are many fully reasonable and legitimate situations
  26.    in which one needs to know this.  And C++ is almost the only OOPL
  27.    I know of in which you cannot ask the dynamic type of an object.
  28.  
  29. At the risk of starting up a whole RTTI discussion again, let me try
  30. to clarify what I mean.  I said there _may_ be something wrong with
  31. the design if you are trying to ask the dynamic type of an object.  I
  32. still believe that the design is _likely_ to be flawed, especially if
  33. it being constructed by a beginning C++ programmer.
  34.  
  35. If your design requires a downcast that might be unsafe, you need a
  36. way to check whether the downcast is OK.  However, in the LISP world I
  37. have seen many programs which used dynamic type-checks where they were
  38. not really appropriate.  When a programmer with experience in some
  39. other object-oriented language has the impulse to ask the dynamic type
  40. of an object, the programmer should stop and consider whether that is
  41. really what is needed.  It is easy to have the impulse if that's the
  42. way you have been coding, even if a bit of reflection would show that
  43. a virtual function identifying a well-defined redefinable operation
  44. would be better than a hard-wired and non-extensible conditional.  In
  45. other situations, the impulse might occur because the type information
  46. has been lost by casting to Object* for storage into a container, when
  47. actually the use of a type-safe template class would be better.
  48.  
  49. C++ is more serious than most object-oriented languages about making
  50. the programmer specify the interface through which an object is to be
  51. accessed, and attempting to enforce that interface through the type
  52. system.  It provides mechanisms such as virtual functions and
  53. templates that go some distance toward keeping type-safe programs
  54. flexible enough to be useful.  It does not currently provide the type
  55. of dynamic type query that was requested.  Out of all the situations
  56. where one is tempted to query the dynamic type of an object, many of
  57. them can be better handled with the existing mechanisms, and some
  58. residue will be left over.  I don't claim that this residual set is
  59. nonempty, and I would be happy to see a limited form of RTTI for that
  60. set.  (Others might disagree, and say that dynamic type-querying is
  61. always a poor design outside of debuggers and such.)
  62.  
  63. However, I don't think programmers should fall back on RTTI until they
  64. have thought about other solutions that are more explicit and more
  65. type-safe.  Is the conditionalized code really implementing an
  66. operation that should be part of the interface of the base type, i.e.,
  67. a virtual function?  Is the type information being thrown away earlier
  68. through inappropriate casting?  Is it really the type that ought to be
  69. queried, or is the code really looking for some behavioral property of
  70. the object that might be implemented by many different subclasses, and
  71. again should be queried through a virtual function?  I have seen a lot
  72. of LISP code that did something or other if (eq (type-of object)
  73. 'frobozz), and hence wouldn't work for subclasses of frobozz.  And so
  74. forth.
  75.  
  76.  
  77.