So here is my goal, I would like to instantiate an object based on a string and an integer. My current thought was this:
1) Create a list of possible objects 2) Each object has a static method that takes a string and integer and returns true, if what was passed matches the static members that object. 3) Then iterate over the list of possible objects, call the static function of that object if it returns true then instantiate that particular object:
pseudocode:
// Pseudo Definitions
class CoolObject
class CoolObject2: public CoolObject ....
class CoolObject3: public CoolObject ....
// List of Objects
std::list<CoolObject> list_of_possible_objects;
list_of_possible_objects.push_back(CoolObject);
list_of_possible_objects.push_back(CoolObject2);
list_of_possible_objects.push_back(CoolObject3);
// Inside of the matching function
for (std::list<CoolObject>::iterator it=list_of_possible_objects.begin(); it != list_of_possible_objects.end(); it++) {
if(*it::is_cool_object(string, int)) {
return *it(string1, string2); //Assume the constructor takes some objects that were passed into the function
}
}
However, this seems like a dream since C++ does not allow to have objects that are not instantiated.
I am sure I can do this with some kinda map and a switch statement but this seemed lot more elegant in my head so I went for it first.
Is there any pattern out there that is similar to this, that I can follow? Or is there a way to make what I am asking be legal?
I would rather not maintain a huge map and I would rather just have a list of objects and ask each object if its the one and then instantiate, if having a huge map and a switch statement is the only way, thats fine. I thought maybe ask some gurus, if this is possible or is there a better way of doing it before giving up on this way.
Thanks for all the help!
Aucun commentaire:
Enregistrer un commentaire