C#有一个语法特性,允许您在一行上将多个数据类型连接在一起。
string s = new String();
s += "Hello world, " + myInt + niceToSeeYouString;
s += someChar1 + interestingDecimal + someChar2;
在C++中该怎么做呢?据我所见,你需要将它们分别放在不同的行上完成操作,因为它不支持使用+运算符连接多个字符串/变量。这样做是可以的,但看起来不太整洁。
string s;
s += "Hello world, " + "nice to see you, " + "or not.";
以上代码会产生一个错误。
char *
指针相加。这就是导致错误的原因 - 因为对指针求和是没有意义的。如下所述,将至少第一个操作数转换为std::string
,就不会出现错误了。 - underscore_d