1

我有一个或多或少遵循这种模式的结构:

struct sTruct {
   int count;
   struct {
       int A;
       int B;
       int C;
   } array[];   //count is the size of this array
};

我希望能够使用以下语法来初始化它们:

sTruct gInit1 = { 2, { {1,2,3},{4,5,6} }};

确实,初始化语法(或者更确切地说,它的紧凑性)比特定的结构布局更重要。我无法访问标准容器(嵌入式平台),但如果需要,我可以复制它们的一些行为。

在最终形式中,我想一次初始化大约 300 个这些结构容器的数组,只是为了添加更多级别的括号。

4

1 回答 1

7

你不能这样做。如果你给数组一个你可以的大小。另一种选择可能是:

template < int size >
struct sTruct
{
  struct { int a, int b, int c } array[size];
};
sTruct<2> gInit1 = {{1,2,3},{4,5,6}};

但是,当然,你所有的结构都是不同的类型,所以它可能不是你想要的。您唯一的其他选择必须是基于免费存储的,并且在 0x 中的初始化列表之前不会为您提供该语法。

于 2010-12-17T17:21:42.527 回答