for my application I am loading from the hardrive a list of different “items”: these “items” are plain-old-data structs and represent the definitive list of all the “items” in my program.
So far what I do is that I simply put all the deserialized instances into one big std::vector and refer to them in the rest of my program by pointers. I am not supposed to have any “item” object anywhere else than in my std::vector that acts as a database.
Is there a good or specific design pattern for this case? I feel like sooner or later I will have item objects elsewhere than in my std::vector or that i’m using a method that is too simple to be robust
thanks
edit : for clarification’s sake, I am doing something like this (from memory):
struct Item {
std::string name;
int property; //etc
}
class Items {
static std::vector<Item> _list;
static void init() {
Item new_item = readfromfile(...);
_list.push_back(new_item)
}
}
If you are passing pointers around, a
std::vectoris a bad idea because the pointer will change when the vector needs to be reallocated. Consider usingboost::ptr_vectorand dynamic allocation.Apart from this minor consideration, a simple vector is the simplest storage possible and as good as any other when iteration is the only concern. I’d advise you to stay with it until other needs arise.