Thread: Casting const char * to STL string

  1. #1
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507

    Casting const char * to STL string

    I had never seen this before and was wondering how it worked.

    Code:
    char * szTest = "Hello World";
    
    if((string)szTest == "Hello World")
    cout << "True";
    prints out true.

    Since casting the pointer to a string doesn't create a string object to compare, how exactly does this work?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by valaris
    Since casting the pointer to a string doesn't create a string object to compare, how exactly does this work?
    It does create a temporary std::string object via the constructor that takes a const char*. Incidentally, szTest should be a const char*, not a char*.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Why doesn't it try to find the "inner c-string" of szTest since it is supposed to be a string object? Why create a temporary object?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by C_ntua
    Why doesn't it try to find the "inner c-string" of szTest since it is supposed to be a string object? Why create a temporary object?
    I am not sure what you mean. szTest is a pointer to char, not a std::string object.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    I ll try to explain with some code then. I am trying to simulate how I believe string works:
    Code:
    class myString
    {
         char* cStr;
         ...
    }
    
    bool operator==(const myString& str, const char* cstr)
    {
        if (strcmp(str.cStr, cstr) == 0)
              return true;
        else
              return false;
    }
    If you do this:
    Code:
    char * szTest = "Hello World";
    if((myString)szTest == "Hello World")
    cout << "True";
    Won't this code be executed:
    Code:
     if (strcmp(str.cStr, cstr) == 0)
    thus getting an error, since str is not a real myString, just a char* casted to a myString?

    Well, obviously I am wrong since this has been tested. But I am not understanding it completely.
    If you could compare a char* with another char* why don't overload the operator == for that occasion and require a cast to string for this to work?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by C_ntua
    thus getting an error, since str is not a real myStringjust a char* casted to a myString?
    The cast creates a temporary, so there is a "real" myString. Besides, it is obvious that str is a "real" myString: the function signature declares it as such (or rather, it declares that str is a const reference to a myString object, but we can gloss over that).

    Quote Originally Posted by C_ntua
    If you could compare a char* with another char* why don't overload the operator == for that occasion and require a cast to string for this to work?
    Because pointer comparison merely compares addresses, not the values at those addresses. In other words, you would be testing if the C-style strings are the same, not whether they are equal. This is why strcmp() or strncmp() would be used for C-style strings.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Hmm, yeah. Got a bit confused about how the casting would work. Thanx for the clarification

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by C_ntua View Post
    I ll try to explain with some code then. I am trying to simulate how I believe string works:
    How std::string works is irrelevant. That's the whole point of OOP.

    Anyway, your question is, "Why does this conversion occur when I request it explicitly?" To which I can only say... Uhhh? Err? Graaahg?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    When you're puzzled about casts, don't use C-style casts. Try the C++ casts and see what happens. For example, the above works as
    Code:
    static_cast<std::string>(szTest) == "Hello, World!"
    If static_cast works, then you know that nothing really weird is going on.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 05:34 PM
  2. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  3. Replies: 7
    Last Post: 06-16-2006, 09:23 PM
  4. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 05:59 AM
  5. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM