home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / cplus / 18319 < prev    next >
Encoding:
Text File  |  1992-12-22  |  3.1 KB  |  77 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!rational.com!thor!rmartin
  3. From: rmartin@thor.Rational.COM (Bob Martin)
  4. Subject: Re: .. ptr_cast ..
  5. Message-ID: <rmartin.725047369@thor>
  6. Keywords: ptr_cast
  7. Sender: news@rational.com
  8. Organization: Rational
  9. References: <1992Dec21.112355.6061@fwi.uva.nl>
  10. Date: Tue, 22 Dec 1992 18:02:49 GMT
  11. Lines: 64
  12.  
  13. koelma@fwi.uva.nl (Dennis Koelma) writes:
  14.  
  15. |Hi,
  16.  
  17. |Does anybody know whether the function "ptr_cast", page 442 in
  18. |"The C++ programming language" by Stroustrup, is or will be supported
  19. |by the Sun compiler ?
  20.  
  21. This proposal has undergone a few changes.  the ptr_cast syntax has
  22. been replaced with regular cast syntax.  I am not certain of the
  23. status of this proposal with the standards committee.
  24.  
  25. |If not, is there some "workaround" library available ?
  26.  
  27. It is not very hard to implement your own RTTI system, although it is
  28. a bit tedious and prone to error.  You might want to look at the
  29. scheme used by the NIH classes, just for educating yourself on one
  30. kind of method.
  31.  
  32. The trick to any kind of RTTI is to be able to answer the following
  33. question:  Is object X derived from class Y.  The answer of this
  34. question must be TRUE if X is an instance of Y, or an instance of any
  35. class derived from Y.  (Consider the complexities that this implies
  36. for MI).
  37.  
  38. In general, to be able to answer this question, you must have some
  39. kind of ID which is unique for every different class.  Then a virtual
  40. function which all objects posess which asks the question.  When the
  41. function executes, it checks the ID of the object with the ID in the
  42. query.  If they match, it returns TRUE, otherwise it returns the
  43. result gotten by calling the same function in each of the base
  44. classes.  At the top of the inheritance hierarchy there is a class
  45. which all other classes must derive from.  Call it "Object".  This
  46. class declares the virtual query function and creates an
  47. implementation which returns FALSE.
  48.  
  49. You can work out the rest of the details for yourself, it is not real
  50. hard.  
  51.  
  52. However, it should be noted that implementing such schemes places a
  53. burden upon your class hierarchy.  Every class must have the ID, every
  54. class must implement your query function, etc, etc.  Third party
  55. classes cannot directly participate, someone must be in charge of
  56. dispensing unique ID codes to prevent the possibility of overlap, etc,
  57. etc...
  58.  
  59. If you get the idea that I don't like to see schemes like this, you
  60. are right.  If at all possible, I would wait for the compiler vendors
  61. to implement the RTTI proposal.
  62.  
  63. Once you have RTTI, the temptation is to use it all the time.  Taken
  64. to its extreme, this will cause you to undo many of the benefits of
  65. OO. You should be very jealous of the secrecy of the type of an
  66. object.  It should be exposed only in the most extraordinary of
  67. circumstances.  There are certainly cases where RTTI is useful, but is
  68. is also very easy to abuse.
  69.  
  70.  
  71.  
  72. --
  73. Robert Martin                        Training courses offered in:
  74. R. C. M. Consulting                       Object Oriented Analysis
  75. 2080 Cranbrook Rd.                        Object Oriented Design
  76. Green Oaks, Il 60048 (708) 918-1004       C++
  77.