home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / object / 3077 < prev    next >
Encoding:
Text File  |  1992-07-30  |  3.9 KB  |  93 lines

  1. Xref: sparky comp.object:3077 comp.lang.eiffel:1029
  2. Path: sparky!uunet!psinntp!internet!sbi!zeuswtc!pascal!rhsu
  3. From: rhsu@pascal.sbi.com (Robert Hsu)
  4. Newsgroups: comp.object,comp.lang.eiffel
  5. Subject: Re: Class methods (was: Re: How to design a data structure library)
  6. Message-ID: <1295@pascal.sbi.com>
  7. Date: 30 Jul 92 16:19:21 GMT
  8. References: <GEOFF.92Jul27100601@wodehouse.flash.bellcore.com> <STEINMAN.92Jul28184519@hawk.is.morgan.com>
  9. Followup-To: poster
  10. Organization: Salomon Brothers Inc. NYC
  11. Lines: 80
  12.  
  13. steinman@is.morgan.com (Jan Steinman) writes:
  14.  
  15. > I think the hang-up on queries or commands is getting in the way here,
  16. > as is the class-object distinction. Objects are created continuously
  17. > and rapidly; many messages need to return newly created objects, and
  18. > if you insist that only classes can return new objecs, you'll get in
  19. > trouble quick!
  20.  
  21.     My understanding of Smalltalk is that, indeed, only classes
  22. can create new objects.  If an instance method returns a new object,
  23. that object must initially have been created by a class object.
  24. Assuming this is true, then the set union operation can simply be an
  25. instance method, and still be considered clean, because it is not
  26. creating a new instance.  Rather it is invoking the class object to
  27. create the new instance and passing it back.
  28.  
  29. > ... aMan and aWoman collaborate to create aChild
  30. > -- they don't ask either Manness or Womanness to do it for them!
  31.  
  32.     Of course, this is an over-simplification.  A lot of
  33. interesting actions must be performed for this to happen, most of
  34. which occurring in the early stages; but most importantly, deep in the
  35. code somewhere, after the member access methods at the beginning, and
  36. before the delivery of the exit message at the end, there must be a
  37. call to the appropriate class object to create a new Man or Woman.  So
  38. even though the appearance is that aMan and aWoman did it on their
  39. own, (and presumably want to take all the credit despite having
  40. enjoyed the experience), in fact a higher, more metaphysical thing
  41. happened - appropriately, invoking a metaclass.
  42.  
  43.     (Upon re-reading the previous paragraph, I am quite impressed
  44. with its profundity, and think I may have unwittingly discovered the
  45. answer to life, the universe, and everything.  But then again I am
  46. easily impressed with my own work.)
  47.  
  48.     Going back to the original issue, there is no need to make the
  49. set union operation specifically a class method simply because it
  50. creates a new instance, since new instance creation will be done by
  51. the class object ultimately.  Given that it does take two existing
  52. sets to have a union, it makes sense for the operation to be an
  53. instance method.  Besides, I don't think that it's possible in
  54. Smalltalk to invoke a class method on one of its instances, so if you
  55. were to make "+" a class method, you won't be able to make it an
  56. operator, meaning you can no longer write:
  57.  
  58.     c := a + b.
  59.  
  60.     Someone mentioned that C++ does not have class methods.  C++
  61. does have static member variables and functions, which are pretty much
  62. equivalent to Smalltalk class variables and methods.  C++ also offers
  63. a solution to this problem for those who insist on considering set
  64. union a global operation: you simply implement a global operator for
  65. it that does not belong to the Set class.  Of course you can also
  66. implement it as a regular member function:
  67.  
  68. class Set {
  69. ...
  70.     Set operator+(const Set&);                     // as member function
  71.  
  72.     friend Set operator+(const Set&, const Set&);  // as global operator
  73. ...
  74. };
  75.  
  76.     Either way, you invoke it like this:
  77.  
  78.     c = a + b;
  79.  
  80.     To take a real C++ example, the C++ Standard Components from
  81. USL contains a Set class which has (among others) a union operation
  82. overloaded by the "|" operator, implemented as a regular member
  83. function (i.e. invoked on an instance).  Using it, one would write:
  84.  
  85.     c = a | b;
  86.  
  87. ------
  88. Robert
  89.  
  90. -- 
  91.     // Robert Hsu -- Salomon Brothers Inc. -- rhsu@aristotle.sbi.com \\
  92.     \\   DISCLAIMER: Opinions expressed are not necessarily mine     //
  93.