在初始化列表初始化一个常量大小的数组 [英] Initialize a constant sized array in an initializer list

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

问题描述

我有,可以在以下概括的情况:

I've got a situation which can be summarized in the following:

class Test
{

    Test();

    int MySet[10];

};

是否有可能在初始化列表来初始化 MYSET

喜欢这种初始化列表的:

Like this kind of initializer list:

Test::Test() : MySet({1, 2, 3, 4, 5, 6, 7, 8, 9, 10}) {}

有什么方法来初始化类的initalizer列表中选择一个固定大小的成员数组?

Is there any way to initialize a constant-sized member array in a class's initalizer list?

推荐答案

而在C ++ 03,C ++ 11引入不可用的扩展初始化列表的。如果使用编译器符合C ++的11个标准,你确实可以做到这一点。

While not available in C++03, C++11 introduces extended initializer lists. You can indeed do it if using a compiler compliant with the C++11 standard.

struct Test {
    Test() : set { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } { };
    int set[10];
};

以上code。使用编译罚款 G ++ -std =的C ++ 0x -c test.cc

由于在下面的评论我指出了一个有用的用户来说,这code 无法编译使用微软的VC ++编译器,CL。也许有人可以,如果使用相当于告诉我的std ::阵列会?

As pointed out below me by a helpful user in the comments, this code does not compile using Microsoft's VC++ compiler, cl. Perhaps someone can tell me if the equivalent using std::array will?

#include <array>

struct Test {
  Test() : set { { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } } { };
  std::array<int, 10> set;
};

这也是使用编译好 G ++ -std =的C ++ 0x -c test.cc

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

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