Friday, September 13, 2013

Understanding C++ String Concatenation

Understanding C++ String Concatenation

In C++, I'm trying to understand why you don't get an error when you
construct a string like this:
const string hello = "Hello";
const string message = hello + ", world" + "!";
But you do get a compile time error with this:
const string exclam = "!";
const string msg = "Hello" + ", world" + exclam
Compile time error is:
main.cpp:10:33: error: invalid operands of types 'const char [6]' and
'const char [8]' to binary 'operator+'
Why is the first run fine but the second produce a compile time Error?

No comments:

Post a Comment