I'm trying to create an item and box system, where a box can have a flexible amount of items inside it (i.e. not wasting memory by making every box have 50 items, when some will have just a few). I am not quite sure how to do it.
I tried to put the Item struct inside the Box struct as a flexible array, but it doesn't really work.
Here's my code:
struct Item {
char name[64];
char slot;
int weight;
int size;
int dmg;
};
struct Box {
int size;
Item items[size];
};
int main()
{
Item sword = { "Sword", 'W', 20, 8, 5};
Box box = { 3, (sword, sword, sword) };
}
std::stringand especiallystd::vector?std::vector<Item>seems to do exactly what you need.struct Box {Item* item}; Box.item = new Item[3]Before using vector you must understand how pointers work.