Not sure if anyone's pointed this out already or not, as skimming through the posts it appears most of them are just reiterating the same thing over and over
Anyway, there is a method called polymorphism which can be used in this situation, though not very stylistically/programmatically appreciated.
Say you had a set of classes: apple, carrot, pizza, etc... and they're all food. So you have a base class called the "food" class:
Code:
class food
{
//implementation with virtual functions and etc...
virtual void doFoodStuff()=0;
};
This class can then be "inherited" by your food classes to create the children classes:
Code:
class apple : public food
{
//Now you actually implement the food functions
void doFoodStuff();
};
void apple::doFoodStuff()
{
// stuff
}
And then, say you wanted the user to input what type of food they want to interact with, you could do the following:
Code:
<grab user input for a name>
food* foodItem=0
if(!strcmp(foodname,"apple"))
foodItem=new apple();
else if(<etc>)
else
// error
foodItem->doFoodStuff(); // This will now execute whichever item of food's function that the user asked for
Polymorphism is a very fun concept. This, however, is only one solution to the problem at hand, and another (possibly) better solution would be to have a class behave much like a relational array, where when you create the class you give it a name:
Code:
class myCustomClass
{
private:
char** classList;
public:
int createNewClass(char* className);
};
Then the implementation of the constructor function would associate the class with a new id in the classList array. Then from that point on, you could have an associative array (overloaded of course) so that whenever you do [myClass] where myClass is a char* it will automatically pull the class's variables that you're looking for.
Hopefully this hasn't been a bit too over-the-top and you can get something from it. See my article on Module Design for Game Development and also the discussions following it for more information on polymorphism.