Re: basic_string ctor



Carl Daniel [VC++ MVP] wrote:
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.

I don't consider this reasonable at all. In fact I can't think of a time where I've ever written code that might possibly pass null into a string method, nor code that had to explicitly check for null first. 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. If std::string supported a "null" string value, maybe it would make more sense but I've never been a fan of "magical states".


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.

I agree with this, which is why I agree with Java's philosophies. What I disagree with is that string should allow nulls. No function should allow null unless there is a good reason or at least a very well-definined and intuitive reason to allow nulls (of which there are quite a few, but a lot less often than people would think).


If anything I'd want the std::string ctor to have "defined" over "undefined" behavior but I'd want that definiton to be an exception thrown (which basically guarantees your program will crash).

Jason
.



Relevant Pages

  • Re: a method to make js have the ability to inherit
    ... but without the implied type-conversion of the string ... that uses the name of a specific constructor. ... programmer has no idea at all what types of object they are ... no reason for ever doing so. ...
    (comp.lang.javascript)
  • 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: Newbie: does not have a parameterless constructor
    ... Conversation: Newbie: does not have a parameterless constructor ... private string mReason; ... public TUsers(string Reason, string Reid) ...
    (microsoft.public.dotnet.framework.aspnet.webservices)
  • 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)

Loading