这个转换让我感到困惑:
#include <string>
#include <iostream>
#include <memory>
using namespace std;
int main() {
string str1 = (string)"I cast this thing" + " -- then add this";
cout << str1 << endl;
}
有人能解释一下为什么这个C++风格的字符串强制转换是有效的(或者被允许)吗?我将生成的优化汇编代码与以下内容进行了比较:
string str1 = string("I construct this thing") + " -- then add this";
它们看起来是完全一样的,因此我感觉自己忘记了一些C++语法,这些语法实际上允许这种强制类型转换/构造互换。
std::string str2 = std::string("I construct this thing") + " -- then add this";
(T)x
和T(x)
两者在意义上是相同的,只是名称不同(C 风格的强制类型转换和函数风格的强制类型转换)。 - Konrad Rudolph