home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / cplus / 12886 < prev    next >
Encoding:
Internet Message Format  |  1992-08-25  |  2.9 KB

  1. Path: sparky!uunet!cs.utexas.edu!sun-barr!ames!agate!agate!matt
  2. From: matt@physics16.berkeley.edu (Matt Austern)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Novice Questions
  5. Date: 25 Aug 92 19:30:32
  6. Organization: Lawrence Berkeley Laboratory (Theoretical Physics Group)
  7. Lines: 45
  8. Distribution: usa
  9. Message-ID: <MATT.92Aug25193032@physics16.berkeley.edu>
  10. References: <1992Aug24.235755.28424@athena.cs.uga.edu>
  11.     <1992Aug26.001150.8237@sunb10.cs.uiuc.edu>
  12. Reply-To: matt@physics.berkeley.edu
  13. NNTP-Posting-Host: physics16.berkeley.edu
  14. In-reply-to: pjl@sparc2.cs.uiuc.edu's message of Wed, 26 Aug 1992 00:11:50 GMT
  15.  
  16. In article <1992Aug26.001150.8237@sunb10.cs.uiuc.edu> pjl@sparc2.cs.uiuc.edu (Paul Lucas) writes:
  17.  
  18. > >syntax?  I'm writing a simple program that does different types of string
  19. > >reversing and thought that I should call a function with a string, and then
  20. > >return that string reversed in different ways.
  21. > *****>    Array names (almost) always decay into a pointer to the first
  22. >     element, so _that's_ what you return--a pointer.
  23.  
  24. Yes and no.  This answer is correct, but, depending on what the
  25. original user wanted, not necessarily useful.  My impression was that
  26. he wanted to treat arrays (including strings, which, in C, are just
  27. arrays of characters) as first-class objects.  Pointer manipulation is
  28. useful, but it isn't the same thing at all.
  29.  
  30. (And one problem with returning a pointer to an array instead of
  31. returning an array is just that a pointer has to point to something!
  32. As people have already observed in the thread on garbage collection,
  33. this means that when a function returns a pointer, part of the
  34. interface of that function must be an explicit protocol on who
  35. allocates and deallocates that storage.)
  36.  
  37. For example: if arrays were first-class objects, then, if A is an
  38. array, and foo() was a function which returns an array, you could
  39. write 
  40.         A = foo().  
  41. Of course, this isn't possible with the built-in C types; that is, if
  42. A is declared as "double A[NUM_ELTS]", and foo is declared as "double
  43. *foo()", then this code is illegal.
  44.  
  45. Fortunately, this is only half of the story.  I have carefully written
  46. "C" up until now instead of "C++", because there is a better way in
  47. C++: you can declare your own types, e.g., Array, or String, and you
  48. can treat these types as first-class data types---you can use the
  49. assignment operator for objects of type Array or String, and you can
  50. return them from functions.  
  51.  
  52. The only down side of this is that you have to define Array and String
  53. yourself, or pick them up from some class library; there isn't yet a
  54. Standard Class Library that is defined along with the C++ language.  
  55. --
  56. Matthew Austern                   Just keep yelling until you attract a
  57. (510) 644-2618                    crowd, then a constituency, a movement, a
  58. austern@lbl.bitnet                faction, an army!  If you don't have any
  59. matt@physics.berkeley.edu         solutions, become a part of the problem!
  60.