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