simple question on std::string

This is a discussion on simple question on std::string within the C++ Programming forums, part of the General Programming Boards category; can someone plz explain to me in simple terms why the following code doesn't compile? Code: #include <iostream> int main() ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    47

    simple question on std::string

    can someone plz explain to me in simple terms why the following code doesn't compile?

    Code:
    #include <iostream>
    
    int main()
    {
    const std::string exclam = "!";
    const std::string message = "hello" + ",world" + exclam;
    std::cout << message;
    }
    and why these do?

    Code:
    #include <iostream>
    
    int main()
    {
    const std::string exclam = "!";
    const std::string message = exclam + "hello" + ",world" + exclam;
    std::cout << message;
    }
    Code:
    #include <iostream>
    
    int main()
    {
    const std::string exclam = "!";
    const std::string message = "hello,world" + exclam;
    std::cout << message;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,753
    "hello" + ",world" is an attempt to add C style strings. I thought it might still compile, but oh well

    exclam + "hello" + ",world" results in temporary std::string objects, so there is no attempt to add C style strings.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    >> "hello" + ",world"
    These two strings are constant string literals and string literals have the type const char *. You can't add two pointers together like that.

    You can add a string literal to a string object. That's why "hello,world" + exclam works, because exclam is a string object and "hello,world" is added to it.

    Also, exclam + "hello" + ",world" + exclam probably does exclam + "hello" first, and the result is a string object. That string object is then added to ",world" to create another string object, which is finally added to the second exclam variable.

    You probably won't ever really need something like "hello" + ",world", though, since you can just do "hello,world" as you saw.

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    This would mostly likely work just fine:
    Code:
    const std::string message = std::string("hello") + std::string(",world") + exclam;
    Todd

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    If you look at the operator+() functions defined in <string> (which you forgot to include), they require a std::string on either the left or right side.
    You could try to define your own operator+() which takes a char* on both sides and returns a std::string...

  6. #6
    Registered User
    Join Date
    Nov 2007
    Posts
    47

    cool i understand!

    that was a little confusing and i know it's a unusual question , but thanks for helping anyways guys.. makes sense!

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Quote Originally Posted by cpjust View Post
    If you look at the operator+() functions defined in <string> (which you forgot to include), they require a std::string on either the left or right side.
    You could try to define your own operator+() which takes a char* on both sides and returns a std::string...
    No, you can't. Every operator must have at least one user-defined argument type, and char* is not user-defined.

    Quote Originally Posted by MegaManZZ View Post
    that was a little confusing and i know it's a unusual question , but thanks for helping anyways guys.. makes sense!
    It's not an unusual question. It's just a weird part of C++. Which actually makes it a rather common question.

    Let's put it in simpler terms. You've been taught to use std::string for strings in C++. That's good. You've been taught that you can create a string like this:
    std::string s = "whatever";
    That's also good.
    However, it can lead to the assumption that "whatever" is of type std::string, and that you can do pretty much everything with it that you can do with a std::string object. That's reasonable, and it's the way it works in Java, for example.
    However, the assumption is wrong. C++ has inherited C legacy, and this means that "whatever" is not a std::string, but actually a const char[9] - an immutable array of characters. There is very little you can do with this array. Mostly, you can assign it to something, like a const char*, or a std::string.

    OK, now the + operator associates left to right. In a + b + c, first a+b is evaluated, then the result + c.
    The + operator only works for two std::strings, or one std::string and one character array. It doesn't work for two char arrays. Thus, if by going left to right through the expression, you find a + where both arguments are char arrays (keeping in mind that a proper + returns a std::string), you have an error.
    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

  8. #8
    "Why use dynamic memory?"
    Join Date
    Aug 2006
    Posts
    186
    you forgot to :
    Code:
    #include <string>
    ...
    "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg."-Bjarne Stroustrup
    Nearing the end of finishing my 2D card game! I have to work on its 'manifesto' though <_<

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    796
    If i might add, great explanation CornedBee, nice stuff to read and understand about the depths of C++. Thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 08:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 08:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 11:15 AM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 05:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21