Contrary to your final comment, this seems to be a perfectly reasonable application for std::vector.
As it stands right now, you're trying to create an array of N objects, but then have some M of those actually be null/empty/nonexistent objects.
The reality is that this is not a fixed size collection at all. It's a variable sized collection up to some specified maximum. At a given time a player might be carrying 1 item or 10, but there's some maximum number of items they can carry. They're not, however, constantly carrying the maximum number of items, and some of those happen to be null items1.
Since what you're dealing with really is a variable sized collection (up to a specified maximum), std::vector fits the bill perfectly. Since you're dealing with an Item class, it's probably reasonable to assume that the actual items will be instances of concrete classes that derive from Item. That being the case, you really need to store (some variety of) pointer in the array, not actual Item objects (Otherwise, every derived item you try to put into the array will be "sliced" to become an actual Item object, not a derived object, when it's stored in the array).
You can do that by creating a collection of unique_ptrs (or various other other smart pointer types, such as shared_ptr), but I'd generally recommend that in such a situation you consider the Boost ptr_vector type instead.
As far as enforcing the size limit goes, you probably want to create a small wrapper class to handle this. It's not exactly a complex task, but it's clearly better to centralize that code in one place, not duplicate the size checking code everywhere you might add an item to an inventory.
template <class T>
class inventory {
ptr_vector<T *> items;
size_t max;
public:
inventory(size_t max) : max(max) {}
push_back(T *t) {
if (items.size() < max)
items.push_back(t);
}
// other miscellany here.
};
It's possible, however, that you don't have a polymorphic hierarchy of Items. Instead, you have just have a single, fixed type for all items in the inventory, each of which has a name and such. If that's the case (i.e., all Items are really the same type of object), there's no reason to mess with pointers or ptr_vector. You really just want a std::vector<Item>. Much like above, however, you undoubtedly still want to centralize the code to enforce the Inventory's maximum size (and probably provide other inventory-specific services as well).
- If you'll allow me a digression, the fact that somebody would even consider such a possibility indicates the degree to which using Java warps people's minds and destroys their ability to think clearly. To paraphrase Dijkstra, teaching Java should be a criminal offense.
inventory[i] = itshould be the one throwing an error.