I have several classes, which we'll call classA, classB and classC to keep things simple, all of which inherit from a single class, which we'll call baseClass. In my code I want to instantiate an object of one of the child types, based on what the user clicks on. So if they click "classA" I want to instantiate an object of type classA, etc. The way I have it working at the moment is that I have a pointer of type baseClass, and the user selection is stored in a string. The string is then run through an if statement which selects the appropriate new statement, something like the sudo-code below:
While this works, it is obviously ugly and not very scaleable. Is there some way I can just make some sort of data structure and just do something like newClass=new classes[selection];? Thanks.Code:baseClass * newClass; char selection[]=<user_selection>; if (selection=="classA") newClass=new classA; else if(selection=="classB") newClass=new classB; else if(selection=="classC") newClass=new classC; //...etc... else //throw some sort of error here


Reply With Quote


Bookmarks