Creating class instances with variables
Alright, I've got a function "AddObject(int type)" that takes types defined in an enum in one of my header files. From there, I have a large switch statement that creates an object based on the type given.
I noticed, however, that the different cases all were handled the same way except for the TYPE of object they created with 'new', and the name of the object type created that I send to my log file.
Is there a way I could get this type variable to select the proper class to create without a switch statement?
if you descend these classes from a common ancestor, you can avoid the switch when creating them.
Tom#
Tom#
Sure, depending on what you're doing, either a simple template, or a more complex enum/functor map should provide such functionality. I can't say without knowing more about the requirements and limitations of what you're trying to engineer.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement