jkerian
jkerian

Reputation: 17016

C++ initialization of struct containing an array

I have a structure that more or less follows this pattern:

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

I would like to be able to initialize these with something like the following syntax:

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

Really, that initialization syntax (or rather, the compactness of it) is more important than the specific struct layout. I do not have access to the standard containers (embedded platform), but I might be able to replicate some of their behavior if needed.

In final form, I would like to initialize an array of roughly 300 of these sTruct containers at once, just to add one more level of parenthesis.

Upvotes: 1

Views: 1021

Answers (1)

Edward Strange
Edward Strange

Reputation: 40859

You can't do it. If you gave the array a size you could. An alternative might be:

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

But, of course, all your sTructs are different types and so it may not be what you want. Your only other alternative is going to have to be free-store based and won't give you that syntax until initialization lists in 0x.

Upvotes: 7

Related Questions