初始化字符串的静态const列表 [英] Initialize a static const list of strings
问题描述
我需要在.h中初始化一个static const std::list<std::string>
.但是,我该怎么办?
I need to initialize a static const std::list<std::string>
in my .h. But, how do I do ?
class myClass {
static const std::list<std::string> myList = {"a", "b", "c"};
}
谢谢.
推荐答案
如何在.h中初始化静态const std :: list?
不,您不能直接这样做.
How do I initialize a static const std::list in my .h?
No you can't directly do that.
要在类定义中初始化const静态数据成员,它必须是整数(或枚举)类型;如果此类对象仅出现在整数常量表达式的位置,则也是如此. 有关更多详细信息,请参见 C ++ 11标准在以下位置.
To initialize a const static data member inside the class definition, it has to be of integral (or enumeration) type; that as well if such object only appears in the places of an integral-constant expression. For more details, plese refer C++11 standard in the following places.
$9.4.2 Static data members and
$3.2 One Definition rule
但是,您可能可以执行以下操作: 如何在头文件中定义const static std :: string?
But, you MAY be able to do something like this: How can you define const static std::string in header file?
这篇关于初始化字符串的静态const列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!