Reputation: 1218
Ok, so I have a text-driven game in C++. I've written a Hero class. I also wrote a base class called Item and a subclass of that called Weapon (the idea is to have Weapons, Potions, QuestItems, etc. that inherit from the Item class). That way I can make my "Hero" have an instance variable inventory of Items. I wanted this to be dynamic in case the Hero gained upgrades for his/her storage space.
So my Hero class has a member/instance variable that is a dynamic array of pointers to Items. I declare this and then loop through it setting each element to a new Item like so:
Item** inventory = new Item*[15];
for (int i = 0; i < 15; i++)
inventory[i] = new Item();
That took place in the Hero class's void Init() function which all the constructors call (flexible construction). Now then, I have tried to write a member function to add an item to the first "empty" slot in the inventory, but that is pretty gritty and couldn't figure out what was crashing my program when I tested it. Ultimately I figured out it was crashing when I tried to access an element of the array. Now I just have this snippet that assumes the first element is empty and tries to "add" the item given to that index in the array:
void Hero::AddToInventory(Item *item)
{
*inventory[0] = item;
}
And here is how I called the method in main():
int main()
{
Hero *player = new Hero("Joe");
Item *primaryWeapon = new Item("Dagger");
player->AddToInventory(primaryWeapon);
return 0;
}
Any help is greatly appreciated; I'm just a little rusty on my dynamic objects and data structures in C++. Thanks in advance!
P.S. I know this can be done simpler with vectors but I have not learned how to use those yet and would like to stick with the dynamic arrays for now.
Upvotes: 0
Views: 2191
Reputation: 3974
First of all, you leak memory by first initializing each array index with a new Item()
and then replacing that allocated item with the actual item later. It is probably better to just have an array of null pointers to begin with.
Item** inventory = new Item*[15];
for (int i = 0; i < 15; i++)
inventory[i] = NULL;
I believe you can also initialize an array to null like this:
Item** inventory = new Item*[15] = {NULL,};
Now, you say you have a subclass of Item
called Weapon
, but I see that you never use it. Once you declare new Item()
, it is of type Item, no longer able to morph into derived class types like Weapon()
. So, in this case, to create a dagger weapon, it should look more like this instead:
Item *primaryWeapon = new Weapon("Dagger");
Lastly, you get the crash during AddInventoryToItem
because you are dereferencing your instance of Item
at the given index, then assigning it to a pointer to an Item
instead of to an Item
directly. Try this instead:
void Hero::AddToInventory(Item *item)
{
inventory[0] = item;
}
Also note that if you just simply replace the inventory index with a new pointer, you could also result in a memory leak. However, if each item was initialized to null instead of a new Item()
, and you only added the item to an empty slot, this won't be a problem.
Upvotes: 1