jke*_*ian 1 c++ static-initialization
我有一个或多或少遵循这种模式的结构:
struct sTruct {
int count;
struct {
int A;
int B;
int C;
} array[]; //count is the size of this array
};
Run Code Online (Sandbox Code Playgroud)
我希望能够使用以下语法初始化它们:
sTruct gInit1 = { 2, { {1,2,3},{4,5,6} }};
Run Code Online (Sandbox Code Playgroud)
实际上,初始化语法(或者更确切地说,它的紧凑性)比特定的结构布局更重要.我无法访问标准容器(嵌入式平台),但如果需要,我可以复制它们的一些行为.
在最终形式中,我想一次初始化大约300个这些sTruct容器的数组,只是为了添加一个更高级别的括号.
你不能这样做.如果你给阵列一个大小你可以.另一种选择可能是:
template < int size >
struct sTruct
{
struct { int a, int b, int c } array[size];
};
sTruct<2> gInit1 = {{1,2,3},{4,5,6}};
Run Code Online (Sandbox Code Playgroud)
但是,当然,你所有的sTructs都是不同的类型,所以它可能不是你想要的.您唯一的另一种选择是必须基于免费存储,并且在0x中的初始化列表之前不会提供该语法.
| 归档时间: |
|
| 查看次数: |
952 次 |
| 最近记录: |