简体   繁体   English

初始化包含带有初始化列表的const数组的Struct

[英]Initialize a Struct containing a const array with initializer list

I working with C++11 and have a Class containing the following Struct: 我使用C ++ 11并拥有一个包含以下结构的类:

struct Settings{
    const std::string name;

    const std::string* A;
    const size_t a;
};

class X {
    static const Settings s;
    //More stuff
};

In the .cpp file I want to define it like this .cpp文件中,我想像这样定义它

X::s = {"MyName", {"one","two","three"}, 3};

But this does not work. 但这不起作用。 However it does work using an intermediate variable 但是它确实可以使用中间变量

const std::string inter[] = {"one","two","three"};
X::s = {"MyName", inter, 3};

Is there a way to do it without the intermediate variable? 有没有办法在没有中间变量的情况下做到这一点?

A pointer cannot be initialized from a list of values. 无法从值列表初始化指针。 You could use std::vector instead: 您可以使用std::vector代替:

#include <vector>

struct Settings{
    const std::string name;
    const std::vector<std::string> A;
//        ^^^^^^^^^^^^^^^^^^^^^^^^
    const size_t a;
};

You can then write: 然后你可以写:

class X {
    static const Settings s;
    //More stuff
};

const Settings X::s = {"MyName", {"one","two","three"}, 3};

Here is a live example . 这是一个实例

As suggested by Praetorian in the comments , you may want to replace std::vector with std::array , if it is acceptable for you to specify the size of the container explicitly, and if the size does not need to change at run-time: 正如意见建议的执政官 ,你可能需要更换std::vector使用std::array ,如果它是可以接受的,你要明确指定容器的大小,如果大小不需要在运行-改变时间:

#include <array>

struct Settings{
    const std::string name;
    const std::array<std::string, 3> A;
//        ^^^^^^^^^^^^^^^^^^^^^^^^^^
    const size_t a;
};

And here is the live example . 这是现场的例子

That's because you're storing a pointer to an array of strings, not the strings themselves; 那是因为你要存储一个指向字符串数组的指针,而不是字符串本身; so you need an array of strings somewhere to point to. 所以你需要一个指向某个地方的字符串数组。 All you have is a brace-initialiser containing pointers to character arrays. 你所拥有的只是一个包含指向字符数组指针的大括号初始化程序。

If you were to store the strings themselves (in a vector<string> , for example), then the first version would work. 如果你要自己存储字符串(例如,在vector<string> ),那么第一个版本就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从构造函数参数初始化类初始化列表中的const多维数组 - Initialize const multidimensional array in class initializer list from constructor arguments 使用const初始化列表初始化const向量 - initialize const vector with const initializer list 在C ++中的类初始值设定项中初始化const数组 - initialize a const array in a class initializer in C++ 无法使用声明中的初始化列表初始化const char * / string数组的向量 - Cannot initialize a vector of const char*/string array with an initializer-list on declaration 结构数组初始化列表的行为 - Behaviour of Initializer List for Array of Struct 直接使用初始化程序列表初始化仅包含一个数组作为成员的结构是否合法? - Is it legal to initialize a struct with only one array as member using a initializer list directly? 如何使用初始化列表初始化包含 boost::array&lt;&gt; 的 Boost.Fusion 向量 - How to initialize Boost.Fusion vector containing boost::array<> with initializer list 在初始化列表中使用复杂函数来初始化const成员一个好的设计? - Is using a complex function in initializer list to initialize const member a good design? 构造函数中的初始化程序列表,用于初始化私有const c字符串错误 - Initializer list in constructor to initialize private const c string error C++ 对象数组,带有 const 初始值设定项列表 - C++ array of objects with const initializer list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM