-3

可能重复:
两个字符串文字的连接

案例 1 "hello" "world"(无错误)

案例 2 "hello"+"world"(错误)

我知道+运算符必须至少将其操作数之一设为string type,not string literal

问题是,这两种情况都没有意义,因为我们可以将它包含在single literal!

那么为什么允许案例1!

4

2 回答 2

5

情况 1 是允许的,因为 C++ 基本上认为相邻的文字是相同的文字,即代码在解析的早期就"hello" "world"被转换为。"helloworld"

于 2012-09-20T09:48:17.387 回答
0

Case 1, as you call it, is a convenient way to let string constants span several lines or be broken up into easily identified parts.

It's there to make your life easier.

Assuming that + should have the same semantics when used with string constants is a mistake. Especially in C, where string isn't an available type.

于 2012-09-20T09:50:05.290 回答