String Concatenation
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.
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.