String concatenation error question

I'm trying to understand why you get an error in the second one and not the first one. It seems you can't concatenate two string literals to start a new string, which makes sense. Why bother with the last line when you could just do

const string mesg = "Hello, world" + exclam;

But why is it ok to concatenate two string literals just because they aren't the first two things to make up a new string like in the second line of code?

1
2
3
4
5
6
7
// no issues
const string hello = "Hello";
const string message = hello + ", world" + "!";

// error on mesg
const string exclam = "!";
const string mesg =  "Hello" + ", world" + exclam; 


Also, would you say the variables here are string objects?
Last edited on
When you write "Hello " + "world", the compiler interprets this as char* + char*, and adding two pointers together is nonsensical.

So, to concatenate two strings with operator+, you must have at least one string object for the compiler to pick the right overload.
 
const string mesg = string("Hello ") + "world" + exclaim;


And yes, your constant variables are objects of the std::string class.
mesg is illegal because it groups like this:

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

Which as you can see is adding 2 literals before adding to exclam, which you know you can't do.

message groups like this:

(hello + ", world") + "!";

The subexpression forms a temp. string which becomes the (legal) left operand.

When we mix strings and string / character literals, at least one operand to each + operator needs to be a string type.
Last edited on
The string concatenation operator only operates on two items at a time, either stringA + stringB, or string + string literal, or string literal + string. The result of each of these operations is the concatenated string. The operation is left-associative which means it is evaluated from left to right.

On line 3 the right hand side is evaluated first as:

( hello + ", world" ) + "!";

First, evaluate hello + ", world" which yields a string, then concatenate that string with "!". At every stage at least one of the two operands is a string so it works.

On line 7, the first two to be evaluated would be two string literals, but there is no concatenation operator defined for two string literals so it gives an error message. As you pointed out, it isn't necessary as you can just write:

const string mesg = "Hello, world" + exclam;

One other note. It is possible to "concatenate" literal strings by putting whitespace between them (not the + sign). This can be useful if you have very long literal strings. For example,

1
2
3
string mesg = "Four score and seven years ago,"
              "our forefathers brought forth on this continent"
              "a new nation, conceived in liberty and ...";


Note that there are no new line characters in the string (its just one line). Again the only thing that can come between the literal strings is whitespace (spaces, tabs, newlines).
Last edited on
Topic archived. No new replies allowed.