[n00b] Order of strings generates error

Discussion in 'C++' started by Charles, Jan 2, 2007.

  1. Charles

    Charles Guest

    I am learning from the Accelerated C++ book. The following example
    doesn't work and I don't know why:

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


    But if I invert strings, it works:


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

    Unfortunately there's no explanation why. Could you tell me please?
    Thanks.
    Charles, Jan 2, 2007
    #1
    1. Advertising

  2. Charles

    red floyd Guest

    Charles wrote:
    > I am learning from the Accelerated C++ book. The following example
    > doesn't work and I don't know why:
    >
    > #include <iostream>
    > #include <string>
    > int main () {
    > const std::string exclam = "!";
    > const std::string message = "Hello" + ", world" + exclam;
    > return 0;
    > }
    >
    >
    > But if I invert strings, it works:
    >
    >
    > #include <iostream>
    > #include <string>
    > int main () {
    > const std::string exclam = "!";
    > const std::string message = ", world" + exclam + "Hello";
    > return 0;
    > }
    >
    > Unfortunately there's no explanation why. Could you tell me please?
    > Thanks.
    >


    + associates left to right. So in your first case, you are trying to
    add two string literals (char arrays) together, and then add the
    exclamation point. There is no operator+ which takes two const char *'s
    as parameters.

    In the second case, you are "adding" a string literal to a std::string,
    and there is an operator+(const char*, const std::string&) defined,
    which returns a std::string, so then you add another string literal to
    the returned string, using operator+(const std::string&, const char *).
    red floyd, Jan 2, 2007
    #2
    1. Advertising

  3. Charles

    Charles Guest

    red floyd escreveu:
    > + associates left to right. So in your first case, you are trying to
    > add two string literals (char arrays) together, and then add the
    > exclamation point. There is no operator+ which takes two const char *'s
    > as parameters.
    >
    > In the second case, you are "adding" a string literal to a std::string,
    > and there is an operator+(const char*, const std::string&) defined,
    > which returns a std::string, so then you add another string literal to
    > the returned string, using operator+(const std::string&, const char *).


    Ah ok, thank you!
    Charles, Jan 2, 2007
    #3
  4. Charles

    Ron House Guest

    Charles wrote:
    > I am learning from the Accelerated C++ book. The following example
    > doesn't work and I don't know why:


    What do you mean "doesn't work"??? Compiler error? What is it? Runtime
    error, wrong output, what? If you want help, please write a coherent
    account of your problem. Putting "n00b" in the title is not an excuse
    for laziness, only for ignorance.

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


    But luckily, the problem is obvious. The + operator is left-associative.
    Putting + between two C-style literals is attempting to call the
    (nonexistent) operator that adds two char pointers. The above could be
    fixed by writing:

    "Hello" + (", world" + exclam);

    --
    Ron House
    http://www.sci.usq.edu.au/staff/house
    Ron House, Jan 2, 2007
    #4
  5. Charles

    Jim Langston Guest

    "Ron House" <> wrote in message
    news:...
    > Charles wrote:
    >> I am learning from the Accelerated C++ book. The following example
    >> doesn't work and I don't know why:

    >
    > What do you mean "doesn't work"??? Compiler error? What is it? Runtime
    > error, wrong output, what? If you want help, please write a coherent
    > account of your problem. Putting "n00b" in the title is not an excuse for
    > laziness, only for ignorance.
    >
    >> #include <iostream>
    >> #include <string>
    >> int main () {
    >> const std::string exclam = "!";
    >> const std::string message = "Hello" + ", world" + exclam;
    >> return 0;
    >> }

    >
    > But luckily, the problem is obvious. The + operator is left-associative.
    > Putting + between two C-style literals is attempting to call the
    > (nonexistent) operator that adds two char pointers. The above could be
    > fixed by writing:
    >
    > "Hello" + (", world" + exclam);


    You know, this question comes up quite a bit, and it even bit me a few times
    til I fugred it out. I believe that in C and C++ adding two pointers is
    undefined behavior. You can add an int to a pointer, but not a pointer to a
    pointer. So, I've been thinking, would it make sense to make an
    operator+(const char*, const char*) that would return a std::string? What
    would be the drawbacks to this (other than the possible difficulty of
    implementing it).
    Jim Langston, Jan 2, 2007
    #5
  6. Charles

    Ivan Novick Guest

    Jim Langston wrote:
    > You know, this question comes up quite a bit, and it even bit me a few times
    > til I fugred it out. I believe that in C and C++ adding two pointers is
    > undefined behavior. You can add an int to a pointer, but not a pointer to a
    > pointer. So, I've been thinking, would it make sense to make an
    > operator+(const char*, const char*) that would return a std::string? What
    > would be the drawbacks to this (other than the possible difficulty of
    > implementing it).


    Can you give an example where this functionality would be practical to
    use? The example given above is not a good use case because you can
    just do the following and the string literals will be concatenated
    without having to construct an object:

    std::string one = "Santa";
    std::string two = "Hello World " "from " + one;

    ----
    Ivan
    http://www.0x4849.net
    Ivan Novick, Jan 2, 2007
    #6
  7. On Tue, 02 Jan 2007 13:10:10 +1000, Ron House wrote:
    >Charles wrote:
    >> I am learning from the Accelerated C++ book. The following example
    >> doesn't work and I don't know why:

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

    >
    >But luckily, the problem is obvious.


    The problem is obviously not obvious for someone who learns C++ from
    "Accelerated C++".
    Roland Pibinger, Jan 2, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Afonin
    Replies:
    0
    Views:
    489
    Peter Afonin
    Aug 30, 2003
  2. =?Utf-8?B?cmFzMjY=?=

    External Assembly accesing Session generates an error

    =?Utf-8?B?cmFzMjY=?=, May 16, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    421
    =?Utf-8?B?cmFzMjY=?=
    May 16, 2006
  3. RAM
    Replies:
    0
    Views:
    289
  4. Vaibhav Shah
    Replies:
    1
    Views:
    470
    bruce barker \(sqlwork.com\)
    Sep 6, 2006
  5. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    740
    Malcolm
    Jun 24, 2006
Loading...

Share This Page