I haven't done much polymorphism in c++, and it's been a long time since I done it in other languages, so I'd like some input as to if what I'm trying to achieve is even possible.
I have a class "Entity", and several other 'specialized' sub-classes which inherit Entity. These sub-classes all have completely different methods (not overriding/reimplementing the same one), as they need to perform different functions.
I'm looking for a way to organize all of these "Entities" into a single list. If I were to create a single array of type "Entity" and assign the sub classes to the array, I would no longer be able to call any of the sub-class functions, only the parent "Entity" functions.
The only messy way I can immediately think of is to create an array for each sub-class, which doesn't seem that neat at all when I think of the amount of sub-classes I intend to make, not to mention the amount of memory the application would need to allocate.
Is there any way to cleanly achieve something similar to what I'm trying to do?
std::unique_ptrinstances then you can store any heap-allocated subclass ofEntityin the list. When iterating the list you could usedynamic_castto filter for the particular objects you are interested in at that moment.