Re: basic_string ctor



Stephen Howe wrote:
>> They're not the same in C, which has no real concept of a string.
>
> It most certainly does.
> Admittedly it has limitations (can't have a string of '\0''s,
> strlen() is O(N) not O(1), all memory managment of it is manual) but
> it has been like that since the early days.

Which doesn't make it right - C strings are lame at best and we all know it.

>
>> In the (IME far more common) case where the distinction is merely an
>> artifact of C's poor string concept, I'd prefer the C++ library to
>> simply accept 0 as an empty string.
>
> It has nothin to do with C's poor string concept.
> It has everything to do with the fact that NULL or 0 in pointer
> situations is regarded as an invalid pointer for all types.

But here we're converting from a pointer to an object. The validity of that
pointer is entirely up to the object's constructor to decide. I'm sure 10's
of 1000's of C++ programmers have designed classes that take a pointer
paramter to their constructor and provide some reasonable default behavior
if the pointer is null. It's really not that radical an idea.

>
> As shown in answer to Duane, you can make a valid pointer to an empty
> string and a NULL pointer both construct a std::string as empty.
>
> But if you fold this distinction in std::string's constructor, so
> NULL will silently construct an empty std::string, then in the
> situation where the pointer being NULL is an error in the program,
> this will silently pass over this.

Yep. And in the 1000's of cases where the 0 isn't an error it'll frustrate
and confuse the programmer when the seemingly reasonable std::string(0)
crashes their program.

Neither solution is perfect, and opinions vary as to which is "better" or
"safer". Most opinions I've encountered agree that C++ would be easier to
learn and safer for most programmers to use if there was less undefined
behavior.

-cd


.



Relevant Pages

  • Re: Strange EAccessViolation which I cannot figure out...
    ... where in your code you are freeing and setting it to NIL to then later ... The proper way is to always create it in the constructor or, ... type cast around this returned value as a pointer to a pchar string. ...
    (comp.lang.pascal.delphi.misc)
  • Re: Crashes in Debug (random) but not release
    ... The parameter is passed as a contant string (ex: ... an invalid pointer arrives at the copy ... >Who wrote the constructor? ... >>> Boston, MA 02118 ...
    (microsoft.public.pocketpc.developer)
  • Re: Arrays of Class Objects
    ... > In your original constructor one had to pass a 'string'. ... The compiler cannot create an Element Object from a string literal ... Element(const char* arg) ... When you are assigning a pointer to std::string you need to make sure ...
    (alt.comp.lang.learn.c-cpp)
  • Re: basic_string ctor
    ... I'm sure 10's of 1000's of C++ programmers have designed classes that take a pointer paramter to their constructor and provide some reasonable default behavior if the pointer is null. ... I can think of a few times you might want to convert a NULL to a std::string, but IMO those cases are all moot because you want to differentiate between an empty string and a null string. ... No function should allow null unless there is a good reason or at least a very well-definined and intuitive reason to allow nulls. ...
    (microsoft.public.vc.stl)
  • Re: problems with free()
    ... although some programmers consider it good practice to put it there ... string "test\0" into mystr', it says 'make mystr point at the static ... You are actually overwriting the pointer with a new ...
    (comp.lang.c.moderated)

Loading