String concatenation

前端 未结 5 1193
暖寄归人
暖寄归人 2020-12-10 03:22

Why it is possible to do

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

And not possible to do this:

5条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-10 03:44

    In addition to the answers that explain the reason for your observations, I post here how to get rid of the problem (you might have figured this out already).

    Replace

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

    with

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

    so you make sure the first operand is a string and not a const char[].

提交回复
热议问题