You can create a C++ structure with an array within:
struct foo{
int bar[42];
};
What I would like to do is:
struct foo{
std::vector<int> bar(42);
};
Of course this doesn’t compile, but you get the intent. I’m aware of .reserve() and the like,
but I would like to have the space already allocated when declaring a foo.
The reason is that I’m supposed to alter a rather complicated Perl script which is generating C code with arrays within structs. These arrays should be replaced by std::vectors. The script subsequently initializes the arrays depending on an XML file and I would rather not mess around with push_back in the script since the structures are deeply nested (structs of arrays and arrays of structs). The sizes of the arrays do of course vary.
Thanks for your suggestions.
Use a constructor:
Memory for the vector will automatically be allocated when creating an instance. The initialization list initializes members before the constructor body executes.