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

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!news.mentorg.com!scherzo!bcohen
  3. From: bcohen@scherzo.NoSubdomain.NoDomain (Bruce Cohen)
  4. Subject: Re: .. ptr_cast ..
  5. Sender: news@news.mentorg.com (News User)
  6. Message-ID: <1992Dec29.191433.12672@news.mentorg.com>
  7. Date: Tue, 29 Dec 1992 19:14:33 GMT
  8. References: <1992Dec21.112355.6061@fwi.uva.nl> <rmartin.725047369@thor>
  9. Nntp-Posting-Host: scherzo.mentorg.com
  10. Organization: Mentor Graphics Corporation
  11. Keywords: ptr_cast
  12. Followup-To: 
  13. Lines: 73
  14.  
  15. In article <rmartin.725047369@thor>, rmartin@thor.Rational.COM (Bob Martin) writes:
  16. |> ...
  17. |> In general, to be able to answer this question, you must have some
  18. |> kind of ID which is unique for every different class.  Then a virtual
  19. |> function which all objects posess which asks the question.  When the
  20. |> function executes, it checks the ID of the object with the ID in the
  21. |> query.  If they match, it returns TRUE, otherwise it returns the
  22. |> result gotten by calling the same function in each of the base
  23. |> classes.  At the top of the inheritance hierarchy there is a class
  24. |> which all other classes must derive from.  Call it "Object".  This
  25. |> class declares the virtual query function and creates an
  26. |> implementation which returns FALSE.
  27. |> 
  28. |> You can work out the rest of the details for yourself, it is not real
  29. |> hard.  
  30. |> 
  31. |> However, it should be noted that implementing such schemes places a
  32. |> burden upon your class hierarchy.  Every class must have the ID, every
  33. |> class must implement your query function, etc, etc.  Third party
  34. |> classes cannot directly participate, someone must be in charge of
  35. |> dispensing unique ID codes to prevent the possibility of overlap, etc,
  36. |> etc...
  37.  
  38. There is an alternative which reduces the overhead so that only classes
  39. which actively need RTTI need to support it: make an RTTI mix-in class
  40. and have any class that needs RTTI inherit it.  The simplest way I know
  41. of to implement this is with templates, which takes care of generating
  42. the unique ID's and allows third-party classes to be added.  I'd include
  43. some source to demonstrate, but I implemented this scheme at a former
  44. job, and didn't get to take the source code along when I left.  I do
  45. have a short paper describing the general scheme lying around here somewhere;
  46. it was published in the proceedings of the Workshop on Reflection at
  47. OOPSLA '91.
  48.  
  49. The scheme I implemented did more than just provide type ID; it was
  50. really a primitive Meta Object Protocol (MOP) which allowed adding
  51. functionality like printing an object or saving an object description to
  52. a file without having to code the capability into the object's class,
  53. even adding it after the class was completely coded.  So you can do
  54. some fairly powerful things with the type system without any compiler support.
  55. But there are drawbacks: additional overhead and requiring recompilation of a
  56. class (actually its template instance) to add the RTTI mix-in.  And some
  57. of the functionality I'd like to have in a Meta Object Protocol really
  58. requires more help from the compiler than you can get from templates.
  59.  
  60. |> 
  61. |> If you get the idea that I don't like to see schemes like this, you
  62. |> are right.  If at all possible, I would wait for the compiler vendors
  63. |> to implement the RTTI proposal.
  64.  
  65. I agree with this.  The implementation I did was purely a
  66. proof-of-concept, to show that a problem we had could be solved that
  67. way.  I would have been overjoyed to not have to have done it myself, as
  68. implementing it was quite tedious.
  69.  
  70. |> 
  71. |> Once you have RTTI, the temptation is to use it all the time.  Taken
  72. |> to its extreme, this will cause you to undo many of the benefits of
  73. |> OO. You should be very jealous of the secrecy of the type of an
  74. |> object.  It should be exposed only in the most extraordinary of
  75. |> circumstances.  There are certainly cases where RTTI is useful, but is
  76. |> is also very easy to abuse.
  77. |> 
  78.  
  79. Also true.  I see RTTI and MOP techniques as tools for system-level
  80. programmers to implement additional general capabilities for objects
  81. such as persistence, debug support, instance cloning, remote
  82. instantiation, etc., etc.  Most times it should not be visible as RTTI
  83. to the application programmer even when it's being used.
  84. ------------------------------------------------------------------------
  85. Bruce Cohen, Mentor Graphics Corpooration | email: Bruce_Cohen@mentorg.com
  86. 8005 SW Boeckman Road                     | phone: (503)685-1808
  87. Wilsonville, OR 97070-7777                |
  88.