From -6032144687105604006
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,b83a4267f4ac775
X-Google-Attributes: gidf78e5,public
From: Jim McKelvey <mckelvey@bean.jpl.nasa.gov>
Subject: Re: string/cow/algorithm
Date: 1998/08/12
Message-ID: <35D1DA1C.A2B@bean.jpl.nasa.gov>#1/1
X-Deja-AN: 380482978
X-NNTP-Posting-Host: mckelvey-mac.jpl.nasa.gov
Content-Transfer-Encoding: 7bit
Approved: stephen.clamage@sun.com (comp.std.c++)
References: <6qfr41$oue$1@pigpen.csrlink.net> <6qgs0r$92e$1@pigpen.csrlink.net> <35ccc2b4.832003@news3.ibm.net> <6qn19u$sb0$1@pigpen.csrlink.net> <6qoumk$402$1@shell7.ba.best.com>
X-UID: 0000000001
X-Status: $$$T
Content-Type: text/plain; charset=us-ascii
Organization: Jet Propulsion Laboratory - Pasadena CA
Mime-Version: 1.0
Reply-To: mckelvey@bean.jpl.nasa.gov
Newsgroups: comp.std.c++
Originator: clamage@taumet


Nathan Myers wrote:
> 
> John Potter<jpotter@falcon.lhup.edu> wrote:
> >I received some email responses to the original.  Not seeing any
> >posts, I will pass on the information to you and others.  Here is
> >another nasty problem:
> >
> > string s("abc");
> > string::iterator i(s.begin());
> > string c(s);
> > *i = 'z';
> > // What is the value of c[0]?
> >
> >A solution is for strings to have one of three states (shared, unique,
> >unsharable).  You have the first two now.  Any function which returns
> >a non-const reference to internals must take care of making the copy
> >and marking it unsharable.  Any copy function which gets an unsharable
> >to copy must do a deep copy.  In the above, s.begin() would do that
> >for s and the copy constructor would be forced to do a deep copy in
> >creating c.
> 


Maybe I'm missing something, but an object should never return a pointer
or reference to its internals.

If s is a string, an iterator on s would bump the reference counter on
the value of s. This means that if s changes, the iterator still refers
to the previous value of s.

The iterator should not be able to change s. It just represents a
position within s.

So the code becomes something like:

string s("abc");
string::iterator i(s);
string c(s);
s(i) = 'z';
// What is the value of c[0]?

c[0] is 'a'.
The only thing changed is s, which is "zbc".

The call to s(i) can verify that i is indeed an iterator on s. If
desired, it can then update the iterator to refer to the new value of s.
In general though, an iterator should be considered a snapshot.


It seems to me that the problem is really that the string class is
poorly designed.


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]




