jeudi 27 avril 2017

Create an inherited class dynamically, access using base class pointer using std::map?

Not sure I can do this. I have question that is a variant of the question linked below.

Can you create a std::map of inherited classes?

Basically the answer to the question below is to create a map of pointers to inherited classes, where the map is searchable based on the first map entry. Then you call use a pointer to address any virtual function you want.

I have implemented a command processor application with a few commands. The map is: std::map cmd_map;

All good so far. However we are updating our code to provide potentially hundreds of derived classes. I don't want to create them all in a factory class at the start of the code, too much memory use.

I want to construct an inherited class dynamically when I need it, then delete using the base class pointer. I want to avoid a big case statement to call each constructor.

Is there a way I can create a std::map or use another STL container that can lookup what constructor to call? Then when I need the derived constructor I can lookup this map to find the correct derived constructor to call.

Apologies if this is a stupid question... Cheers, Paul

Aucun commentaire:

Enregistrer un commentaire