I was wandering if I could get the opinion of someone who might know which is the best container from the STL to use that meets the following requirements:
1. It needs to be able to store objects of different types but all derived from the same base class. An example would be Item->Weapon->Club where Item is the base class which is being store in the container. In this case the container would not have issues with different types of weapons OR items (Such as potion) being added.
2. I need to be able to know the type of a given element during run time. For example it would need to know that element 1 stored a club whereas element 2 stored a bow.
I am also interested in ways around point number two. I know that I could add the functions in the base class so as to make it a common interface but it becomes hard when you have multiple inheritance as far as I can see. Would a linked list be okay to do this with? I have been trying to do it with a linked list but am having some issues.
According to what you have described you would have a abstract base class and a number of different classes that inherit from this abstract base class. You then create a container for this abstract class which can then conatin any type derived from your base class. When you iterate through your container you determine at runtime what each instance in the container is.
As for which type of container you need, is determined on what you are doing with it, if you just need a conatiner a vector will suffice, or if you need to access a specific instance from the container then a map might be suitable; a map stores instances with a unique key and you access the instance from the may via the key.
Hopefully the following code will show you what I am describing. [I am using a vector as a container of my different types]: