tags:

views:

163

answers:

3

Why it is possible to do

const string exclam = "!";
const string str = exclam + "Hello" + " world";

And not possible to do this :

const string exclam = "!";
const string str = "Hello" + " world" + exclam;

I know (although can't understand why) that it is not allowed to do :

const string str = "Hello" + " world" + "!";

as it will be interpreted like const char[6] + const char[6] + const char[1], so from other side, why this is not allowed also, or why it uses char[] and not string.

+3  A: 

It's because you are concatanating const char[6] + const char[6], which is not allowed, as you said.

In C++, string literals (stuff between quotes) are interpreted as const char[]s.

You can concatenate a string with a const char[] (and vice-versa) because the + operator is overridden in string, but it can't be overridden for a basic type.

klez
String literal are funny things. They aren't `const char []` s, they are `const char *` s which point to a statically-allocated area of memory which contains the contents of the literal. But they can act as an initializer to a `const char []`, in which case no extra static memory is allocated. It's a nasty C hangup. [In C they're worse, because they're non-`const` `char *` pointers, but the memory they point to is not guaranteed writable. Horror!]
Philip Potter
@Philip: A string literal has the type `const char[N]` where `N` is the size of the string literal. It is an array, and like an array it can be implicitly converted to a pointer to its first element (a `const char*`). In C++, while the conversion of a string literal to (non-`const`) `char*` is deprecated, it is still allowed.
James McNellis
+11  A: 

The + operator is left-associative (evaluated left-to-right), so the leftmost + is evaluated first.

exclam is a std::string object that overloads operator+ so that both of the following perform concatenation:

exclam + "Hello"
"Hello" + exclam

Both of these return a std::string object containing the concatenated string.

However, if the first two thing being "added" are string literals, as in:

"Hello" + "World"

there is no class type object involved (there is no std::string here). The string literals are converted to pointers and there is no built-in operator+ for pointers.

James McNellis
@sbi : Operators never get evaluated, operands do.
Prasoon Saurav
@Prasoon: A valid nitpick which I can't do anything about because I cannot edit that comment anymore. (However, you don't answer my implied question.)
sbi
@sbi: Such operators are called "left associative" in English too. When I originally posted, I couldn't remember whether "X associative" meant the "X-most" operator was evaluated first or last (where "X" is "left" or "right"). Thanks. @Prasoon: Technically, _expressions_ get evaluated :-)
James McNellis
@James: Thanks, now I know for sure! P.S.: I removed my comment, because now you incorporated in into your answer.
sbi
+2  A: 
const string exclam = "!";    // Initialize a c++ string with an ansi c string
const string str = exclam + "Hello" + " world"; // Using the operator+ method of exclam

You can do it because the operator+ of exclam will return a new string containing "!Hello", on which you subsequently call the operator+ with " world" as parameter, so you get another string which, finally, gets assigned to str by means of the copy constructor.

On the other hand

const string str = "Hello" + " world" + exclam;

cannot be executed because "Hello" is just a const char *, which doesn't have a operator+ taking a const char * as parameter.

Dacav