我知道对于两个
此外,静态字符串连接(不使用+运算符)也可以很好地工作。
为什么两个
const char*
,std::string
的operator +没有重载。因此,std::string str = "Hello" + " World";
不起作用;而下面的代码可以完美运行。
std::string hello = "Hello";
std::string str = hello + " World";
此外,静态字符串连接(不使用+运算符)也可以很好地工作。
std::string str = "Hello" " World";
为什么两个
const char*
不能使用 operator + 进行运算,有什么技术原因吗?
operator+
只被重载用于std::string
。const char*
不是std::string
。const char*
定义一个+perator
的技术原因是什么?” 你无法重载内置类型的运算符。"Hello" " World"
)是在编译时完成的。+操作符的调用仅在运行时完成。 - Jabberwockyusing std::string_literals
才能正确运行:"Hello"s + "world"
。 - Konrad Rudolph