From 6031554238648520563
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,b83a4267f4ac775
X-Google-Attributes: gidf78e5,public
From: phalpern@truffle.ma.ultranet.com (Pablo Halpern)
Subject: Re: string/cow/algorithm
Date: 1998/08/21
Message-ID: <35e1e884.47892348@news.ma.ultranet.com>#1/1
X-Deja-AN: 383385771
X-NNTP-Posting-Host: truffle.ultranet.com
Content-Transfer-Encoding: 7bit
Approved: stephen.clamage@sun.com (comp.std.c++)
References: <6qfr41$oue$1@pigpen.csrlink.net> <6qnj99$h2l$1@nnrp1.dejanews.com> <6qo3fq$dum$1@pigpen.csrlink.net> <35d04855.2117468@news3.ibm.net> <6qptos$a0u$1@news.lth.se> <6qqbvs$adv$1@nnrp1.dejanews.com>
X-UID: 0000000001
X-Status: $$$T
X-Ultra-Time: 21 Aug 1998 03:53:29 GMT
Content-Type: text/plain; charset=us-ascii
X-Complaints-To: abuse@ultra.net
Organization: UltraNet Communications , an RCN Company http://www.ultranet.com/
Mime-Version: 1.0
Newsgroups: comp.std.c++
Originator: clamage@taumet


AllanW@my-dejanews.com wrote:

>Of course, whenever the user gets a reference or address of a
>character in the string -- no matter how (operator[], or dereferencing
>an iterator) -- taking and holding the address is an absolute no-no.
>This isn't just for ref-counted strings, but also for any other
>container that moves things without an explicit request to do so
>(such as vector).

Not quite right.  There are specific operations that invalidate
references to elements.  Copying the container is not one of them. That
is why operator[] (and begin() and end()) must mark the buffer as
unshareable. Otherwise, a copy of the container could be modified
through a (still valid) element reference that was taken before the copy
was made:

	string A("hello world");
	char& r = A[3];   // get and hold a reference to 4th char
	string B(A);      // B had better not share a buffer with A!
	                  // r is still a valid reference.
	r = 'n';	  // Should modify A[3] but not B[3]


-------------------------------------------------------------
Pablo Halpern                   phalpern@truffle.ultranet.com

I am self-employed. Therefore, my opinions *do* represent 
those of my employer.


[ 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              ]




