[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?
    Charles, Jan 2, 2007
    1. Advertisements

  2. Charles

    red floyd Guest

    + 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
    1. Advertisements

  3. Charles

    Charles Guest

    red floyd escreveu:
    Ah ok, thank you!
    Charles, Jan 2, 2007
  4. Charles

    Ron House Guest

    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.
    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, Jan 2, 2007
  5. Charles

    Jim Langston Guest

    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
  6. Charles

    Ivan Novick Guest

    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 Novick, Jan 2, 2007
  7. The problem is obviously not obvious for someone who learns C++ from
    "Accelerated C++".
    Roland Pibinger, Jan 2, 2007
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.