Saturday, 17 August 2013

Initialize a vector of customizable structs within an header file

Initialize a vector of customizable structs within an header file

Is there any way to have a list of global structs and initialize a vector
containing modified version of them from within the same header file? I
know I can't directly access and edit variables from an .h file, since
it's not run-time code, but maybe there happens to be a workaround to this
- or maybe some very basic way that I just happened to skip on the C++
Beginners manual.. pardon me if so!
To make an example, let's say I have a struct which comprehends a couple
of members, and declared some global ones in the .h file..
struct MyStruct
{
unsigned int a;
string foo;
float myfloat;
};
MyStruct struct_one={1,"hello",0.238f};
MyStruct struct_two={10,"salve",3.14f};
MyStruct struct_three={3,"bonjour",0.001f};
MyStruct struct_four={6,"dias",5.0f};
I can then initialize a vector containing them this way (don't know if
it's the best one though)
MyStruct MyStructArray[] = {struct_one, struct_two, struct_three,
struct_four};
vector<MyStruct> MyStructVector(MyStructArray,
MyStructArray+sizeof(MyStructArray)/sizeof(MyStructArray[0]));
But I would like to be able to change, on the fly, some of the structs'
members before creating the vector (or the array) without changing the
global ones. Possible?

No comments:

Post a Comment