Retrieve a pointer type, and use it to cast?

I have one parent class of BaseItem, and ~200 child classes from it.

I have an array of BaseItem*, and right when the program starts, that array is filled with pointers to a single New child class - one of every unique child. It's basically an item index, so an ID can be used to look-up a unique child object type.

What I'd like to do, is: In another area of the code, I have an ID, and a BaseItem*. I'd like to, using the ID, look up the "object pointer type" in the BaseItem* index mentioned above, and then immediately cast my local BaseItem* to "whatever pointer type was discovered" in the item index.

Since I have so many items, I'd like to avoid a long if/else (where I could check with dynamic cast and the ID wouldn't be needed), or a long Switch statement (where I would cast based on the ID, and the item index array would be unnecessary). The Switch is probably the faster of these two ways, but still, I'd like to avoid having a 200+ switch statement if there is nice way to just grab the pointer type.

Thank you : )
Last edited on
What are you trying to accomplish?
Did you create 200 child classes yourself?
Can you show me a few dozens of them? Along with the BaseItem class too.
In general terms, if the behaviour you're programming depends on whether you're using a pointer-to-base or a pointer-to-child, you're doing something wrong and a rethink of your design is in order.

If your code will do one thing with an object of type base, and a different thing with an object of type child, that decision and that behaviour should live within the base/child object , and not in the code handling it.





Thanks, based on what was said, I redid the design. :)
Topic archived. No new replies allowed.