如何在初始化列表中初始化正常数组成员变量? [英] How can I initialize normal array member variable in initializer list?

查看:343
本文介绍了如何在初始化列表中初始化正常数组成员变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想执行以下操作:

class MyClass {
    public:
        MyClass() : arr({1,2,3,4,5,6,7,8}) {}

    private:
        uint32_t arr[8];
};

但不起作用(编译器:'token'之前的主表达式。我看过其他的SO问题,人们传递的事情像 std :: initializer_list ,并尝试有趣的事情,如将数组初始值设置在双大括号,如:

but it doesn't work (compiler: expected primary expression before '}' token.). I've looked at other SO questions and people were passing around things like std::initializer_list, and trying interesting things like placing the array initializer in double braces like so:

MyClass() : arr( {{1,2,3,4,5,6,7,8}} ) {}

但我不熟悉 std :: initializer_list 并且我不太确定为什么在上面的代码中有双括号(虽然它不工作,所以我不知道为什么它很重要)。

but I'm unfamiliar with the purpose of std::initializer_list and also I'm not quite sure why there's double braces in the above code (though it doesn't work anyway, so I'm not sure why it matters).

在构造函数初始化器列表中有没有正常的方式来实现我的 arr 变量的初始化?

Is there a normal way to achieve initialization of my arr variable in a constructor initializer list?

推荐答案

您的语法是正确的。或者,您可以说 arr {1,2,3,...}

Your syntax is correct. Alternatively, you can say arr{1,2,3,...}.

你的编译器不支持这个构造。 GCC 4.4.3和4.6.1都(用 -std = c ++ 0x )。

Most likely is that your compiler just doesn't support this construction yet. GCC 4.4.3 and 4.6.1 both do (with -std=c++0x).

这篇关于如何在初始化列表中初始化正常数组成员变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆