std::vector is a fine way to store "dynamic" (as you call them) things like items, but the real benefit of the vector is not that the thing you are storing can change, but that the number of items in the vector can change without minimal effort on your part. To illustrate, were you to have store your
Item objects as an array, you'd have to fix the size of the array at compile-time (
Item items[SIZE];), which means you have a fixed upper limit on items (among other issues, which aren't as relevant to this specific subject and so I will skip over them).
You could also dynamically-allocate the array at runtime (
Item * items = new Item[SIZE];) which would let you resize the array later by allocating new storage, copying the items over, and deleting the old storage. This, however, is a lot more work for you to do.
Fortunately, that's what
std::vector does for you -- it is a dynamically-sized array implementation, essentially, and handles the memory management for growing the array beyond its current capacity for you. It's definitely the option I'd suggest you pursue for now -- but note that you'll still want to create an
It's very easy to use:
// Create an item list and two item objects to add to it.
Item fireballPotion("Potion of Fireball");
Item healingPotion("Potion of Healing");
// Add the items:
// operator is supported for accessing items.
// This will print "Potion of Fireball" for example:
std::cout << items.GetName();
The MSDN documentation for the
vector class is probably worth a read, and if you aren't familiar with templates -- which is what
vector uses to allow it to store "anything" -- you should brush up on the basics there as well. Which also leads me to a final point: while it looks like a vector can store anything, it does have constraints for what is allowed in it and these sometimes throw beginners. In particular, the type you store in a vector must be copyable, because the vector will need to make a copy of the objects when resizing its internal storage, for example. C++'s rule of three is something to keep in mind here.