Reputation: 710
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?
EDIT: By "within an header file" I meant, "within an header file". If I do unsigned int a = 100;
in the header, I don't have to initialize or call something in the actual source to make it work. The vector by itself works perfectly, I only wanted to know if there was a way to build it from modified versions of the original global structs.. say, I want to use the same global structs, but with different values for member a
.
Upvotes: 3
Views: 2102
Reputation: 7249
On top of @Sam 's answer...
Use a constructor:
struct MyStruct {
unsigned int a;
string foo;
float myfloat;
MyStruct(unsigned int a, const string& foo, float myfloat) : a(a) , foo(foo), myfloat(myfloat) {}
};
Now you can add your struct to the vector with a simple statement
vec.push_back(MyStruct(1, "hello", 0.238f));
class Foo {
public:
static std::vector<int> MyStructVector;
}
inline std::vector<MyStruct> MakeVector()
{
std::vector vec;
vec.push_back(MyStruct(1, "hello", 0.238f));
//...
return vec;
}
std::vector<MyStruct> Foo::MyStructVector= MakeVector();
Upvotes: 2
Reputation:
I do not know if i get your question right:
#include <vector>
#include <iostream>
// Header
// ======
struct MyStruct
{
unsigned int a;
std::string foo;
float myfloat;
MyStruct(unsigned a, const std::string& foo, float myfloat)
: a(a), foo(foo), myfloat(myfloat)
{}
};
typedef std::vector<MyStruct> MyStructs;
extern MyStructs& get_structs();
struct AppendToMyGlobalMyStruct {
AppendToMyGlobalMyStruct(unsigned a, const std::string& foo, float myfloat) {
get_structs().push_back(MyStruct(a, foo, myfloat));
}
};
// Source
// ======
// Have initialization done at first function call, only:
MyStructs& get_structs() {
static MyStructs s;
return s;
}
// Another source
AppendToMyGlobalMyStruct a(0, "0", 0);
// Another source
AppendToMyGlobalMyStruct b(1, "1", 1);
// Another source
AppendToMyGlobalMyStruct c(2, "2", 2);
int main()
{
MyStructs& s = get_structs();
std::cout << s[0].foo << std::endl;
std::cout << s[1].foo << std::endl;
std::cout << s[2].foo << std::endl;
return 0;
}
(names with my are stupid)
Upvotes: 0