c++ - How to make a console inventory system dynamic -
i have inventory system stores "item" types abstract class. have derived class called "skates". program allows user add "skates" vector of "item" pointers. user input model , id of skates , create "skates" object parameters. use "item" pointer point "skates" object , add vector. when user wants edit "skates" object have dynamic_cast "skates" object , edit it.
std::vector<item*> itemcollection; item * item = new skates("model1", 1); itemcollection.push_back(item); //to retrieve skates * skatestoedit = dynamic_cast<skates*>(itemcollection[0]);
the problem facing account new derived class, "skateboard" class example. don't want create new method handle editing of "skateboard" class like:
skateboard * skateboardtoedit = dynamic_cast<skateboard*>(itemcollection[0]);
since mean everytime make new derived class need write same style of code everytime. know if there way make application dynamically know derived class without me needing specify it. needs able determine datatype , prompt user edit whatever property has (since goal users edit object in first place) in 1 dynamic method think should impossible.
i suggest maybe inheritance not need here. instead of having classes skates, skateboard, consider having class item, collection of key values representing properties. way don't care class of item , can list , edit properties dynamically.
Comments
Post a Comment