在c++ 11中初始化C -string
Initialize a c-string in C++11?
如何在c++ 11中初始化C -string ?
I tried:
char* foo = {"bar"};
但是我得到:
ISO C++11 does not allow conversion from string literal to 'char *
因为它必须是指向const
的指针:
const char* foo = "bar";
在c++ 11之前,您可以将foo
简化为char*
,尽管自c++ 03以来已弃用。但是c++ 11删除了弃用,并使其成为非法的。
正确的做法是;
char const* foo = {"bar"};
// ^^^^^ added const
旧的C风格(非const
)已被弃用,现在从c++ 11中删除。
相关文章:
- 为什么 std::string s = "123" 当不涉及副本时被视为复制初始化?
- 初始化有关Google C 样式指南的静态字符串(C-Type或STD :: String)
- 如何使用<string>初始值设定项列表初始化unordered_map<字符串、unordered_set>成员?
- 消除 std::vector<std::string 的列表初始化的歧义>
- 错误 C2440:"正在初始化":无法从"std::string"转换为"双 *[]"
- 初始化不能从const char [3]转换为std :: string *
- 类型为'std::string&的非常量引用的初始化无效,并且从类型为'std::basic_string<char>的右值
- 如何初始化结构字段 std::map<std::string, std::string>称为参数
- <string> 使用双花括号初始化向量
- 从相同的硬编码字符串文字初始化std::string和std::wstring
- 使用双大括号的矢量初始化:std::string vs int
- 初始化 std::string& 的私有类变量
- 特征库 --> 使用文件或现有 std::vector<string> content (c++) 中的数据初始化矩阵
- 正在使用std::string初始化istream属性
- 当数据以二进制零结尾时,如何初始化 std::string
- g++ 使用 boost::algorithm:string:split 和 std::string 时初始化错误无效
- 如何使用包含空值的字符*初始化 std::string
- 类型为 std::String&- 的非常量引用的初始化无效 - 如何删除
- 错误:无法在初始化中将"std::string {aka std::basic_string<char>}"转换为"char*"
- 错误:无法使用初始值设定项列表初始化非聚合类型 'vector<string>'