Topic: A couple of questions about strings


Author: Rich Paul <rpaul@trcinc.com>
Date: 1996/03/26
Raw View
Beman Dawes wrote:
>
>
> >a) Why does basic_string::copy() not also copy an ending '\0'? If
> >there's some sort of good reason, why wasn't a function added that did
> >this (perhaps basic_string::copy_null() or something)? I must say, it
> >is very inconvenient to copy a string to a C-style array of chars with
> >the current string class.
>
> There is such a function - it is called c_str().

well, to be equivilent, it would really be:

strncpy ( buffer, string::c_str(), x );
buffer[x] = 0; // only if strlen(buffer > x), but it's not safe
  // to check that way, since strlening a non-termed
  // char[] is a bad thing
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: willer@carolian.com (Steve Willer)
Date: 1996/03/20
Raw View
I'm probably beating a dead horse here, but I had a couple of
questions about the strings class (at least, the one in the April WP).
Specifically, there are two design decisions that I don't understand.

a) Why does basic_string::copy() not also copy an ending '\0'? If
there's some sort of good reason, why wasn't a function added that did
this (perhaps basic_string::copy_null() or something)? I must say, it
is very inconvenient to copy a string to a C-style array of chars with
the current string class.

b) I don't see any reason why basic_string::copy() is not a const
function. Any particular reason for that one?



[ 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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: kanze@gabi-soft.fr (J. Kanze)
Date: 1996/03/21
Raw View
In article <3150756f.168603020@sqarc.sq.com> willer@carolian.com (Steve
Willer) writes:

|> I'm probably beating a dead horse here, but I had a couple of
|> questions about the strings class (at least, the one in the April WP).
|> Specifically, there are two design decisions that I don't understand.

|> a) Why does basic_string::copy() not also copy an ending '\0'? If
|> there's some sort of good reason, why wasn't a function added that did
|> this (perhaps basic_string::copy_null() or something)? I must say, it
|> is very inconvenient to copy a string to a C-style array of chars with
|> the current string class.

Perhaps because it is so easy to write:

 dst[ s.copy( dst , sizeof( dst ) - 1 ) ] = '\0' ;

I'll admit that I don't particularly care for this style: too many side
effects in the indexing expression for readability.  But it does fit in
with the C/C++ tradition.

|> b) I don't see any reason why basic_string::copy() is not a const
|> function. Any particular reason for that one?

In my copy of the draft, it is const.
--
James Kanze           (+33) 88 14 49 00          email: kanze@gabi-soft.fr
GABI Software, Sarl., 8 rue des Francs Bourgeois, 67000 Strasbourg, France
Conseils en informatique industrielle --
                            -- Beratung in industrieller Datenverarbeitung
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: beman@dawes.win.net (Beman Dawes)
Date: 1996/03/22
Raw View
>I'm probably beating a dead horse here, but I had a couple of
>questions about the strings class (at least, the one in the April WP).
>Specifically, there are two design decisions that I don't understand.
>
>a) Why does basic_string::copy() not also copy an ending '\0'? If
>there's some sort of good reason, why wasn't a function added that did
>this (perhaps basic_string::copy_null() or something)? I must say, it
>is very inconvenient to copy a string to a C-style array of chars with
>the current string class.

There is such a function - it is called c_str().

>b) I don't see any reason why basic_string::copy() is not a const
>function. Any particular reason for that one?

copy() is const in the current WP.


--Beman Dawes
---
[ 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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]