Using std::vector<Item>
is the one and only sane solution here.
In a comment to another answer, you are adding this piece of information:
One problem, however, is that I need to limit the size. Can I do this
with vectors or would I have to manually limit it (e.g not letting it
add more items to the vector once it's too big)
A std::vector
by itself is pretty limitless. Its only real limit is the available memory.
But you should get into the habit of encapsulating container classes anyway. Usually, a container class offers way more functionality than you will ever need for one particular use case. For example, your items
member variable may never require rbegin()
, cend()
, shrink_to_fit()
, difference_type
, get_allocator()
or pop_back()
, just to name a few examples.
It is therefore good practice to create a custom data type which offers only those operations which you really need, and implement the custom data type in terms of std::vector
. It then becomes trivial to implement additional constraints.
Example:
#include <vector>
#include <string>
#include <stdexcept>
#include <exception>
#include <iostream>
// just a super-simple example:
struct Item {
std::string name;
};
// wraps std::vector<Item>:
class Items {
public:
Items(int max_size) :
max_size(max_size),
items()
{
}
void Add(Item const& item) {
if (static_cast<int>(items.size()) == max_size) {
throw std::runtime_error("too many items");
}
items.push_back(item);
}
Item Get(int index) const {
return items[index];
}
private:
int max_size;
std::vector<Item> items;
};
int main()
{
Items items(5);
try {
items.Add({ "sword" });
items.Add({ "shield" });
items.Add({ "torch" });
items.Add({ "axe" });
items.Add({ "boots" });
std::cout << items.Get(3).name << "\n";
items.Add({ "gloves" });
} catch (std::exception const& exc) {
std::cerr << exc.what() << "\n";
}
}
Notice that this example uses exceptions to handle the error. This may not be suitable to your use case; you may consider assert
instead.