home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / cplus / 18307 < prev    next >
Encoding:
Text File  |  1992-12-22  |  3.2 KB  |  67 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: Virtual base classes
  5. Message-ID: <rmartin.725036566@thor>
  6. Sender: news@rational.com
  7. Organization: Rational
  8. References: <724475492snx@trmphrst.demon.co.uk> <449@genie.UUCP>
  9. Date: Tue, 22 Dec 1992 15:02:46 GMT
  10. Lines: 55
  11.  
  12. roger@genie.UUCP (Roger H. Scott) writes:
  13.  
  14. |In article <724475492snx@trmphrst.demon.co.uk> nikki@trmphrst.demon.co.uk writes:
  15. |>In article <445@genie.UUCP> roger@genie.UUCP (Roger H. Scott) writes:
  16. |>> If Kludge is a non-public subclass of WellFormed then there
  17. |>> isn't much point in talking about Kludge redefining WellFormed's virtual
  18. |>> functions since you can't use a Kludge in a place where a WellFormed is
  19. |>> expected (i.e., who would ever have hold of a Kludge and send it one of
  20. |>> WellFormed'd messages, other than a member function of Kludge?).
  21. |>
  22. |>Imagine that WellFormed's constructor adds all WellFormeds to some 
  23. |>Collection of WellFormeds. Notice that all Kludges will also be added to 
  24. |>this collection. Now imagine some calling some virtual function for all 
  25. |>WellFormeds in the Collection.
  26.  
  27. |This seems to raise an interesting question: does there exist an Is-A
  28. |relationship between Kludge and WellFormed, and if not, what does it mean to
  29. |have a collection of objects that *claim* to be WellFormeds but are in fact
  30. |Kludges?  
  31.  
  32. Whether there is an ISA relationship between WellFormed and Kludge
  33. depends upon who you are.  Page 244 of the ARM says: "Members and
  34. friends of class X can implicitly convert an X* to a pointer to a
  35. private immediate base class of X."  Thus, for friends and members of
  36. Kludge, the ISA relationship exists.  But for any other function, the
  37. ISA relationship does not exist.  
  38.  
  39. Thus, a member function or friend of Kludge can convert a pointer to a
  40. Kludge to a pointer to a WellBuilt and return it.  This will allow
  41. other functions to manipulate the Kludge as a WellBuilt.  Its virtual
  42. functions will behave normally.  
  43.  
  44. In Nikki's example, it is the constructor of WellFormed that puts the
  45. newly constructed object onto a list of WellFormed.  Other functions
  46. can then use these objects, not knowing that they are Kludges.  The
  47. virtual functions will be deployed as usual.
  48.  
  49. |Do we see a small whole in the type system here, where within
  50. |member functions inherited from a non-public base class an object which
  51. |is in fact a Kludge can be used interchangably with an object that is a
  52. |WellFormed (the example cited by Nikki uses the base class's constructor, but
  53. |any member function of the base class will do)?  
  54.  
  55. There is no real hole in the type system.  A Kludge object can
  56. masquerade as a WellBuilt as long as the pointer or reference to the
  57. WellBuilt is created by WellBuilt's constructor, or one of the members
  58. or friends of Kludge.  The point is that none of Kludge's clients
  59. knows that it is really derived from WellBuilt.  This fact is
  60. completely hidden from them.  
  61.  
  62. --
  63. Robert Martin                        Training courses offered in:
  64. R. C. M. Consulting                       Object Oriented Analysis
  65. 2080 Cranbrook Rd.                        Object Oriented Design
  66. Green Oaks, Il 60048 (708) 918-1004       C++
  67.