Looking for a way to avoid a massive IF/ELSE and use a lookup table to resolve strings to particular classes to instantiate, that all derive from a base class. Is something like this possible, and if so, how?
typedef struct BaseClass
{
} BaseClass;
typedef struct DerivedClassOne : BaseClass
{
} DerivedClassOne;
typedef struct DerivedClassTwo : BaseClass
{
} DerivedClassTwo;
typedef struct
{
const char *name;
BaseClass class;
} LookupList;
LookupList list[] = {
{"ClassOne", DerivedClassOne},
{"ClassTwo", DerivedClassTwo}
};
BaseClass *InstantiateFromString(char *name)
{
int i;
for (i = 0; i < 2; i++)
{
if (!strcmp(name, list[i].name))
return new list[i].class();
}
}
int main (int argc, char *argv[])
{
BaseClass *myObjectFromLookup = InstantiateFromString("ClassOne");
}