I have 3 object, let's call them Main
, Manager
& Item
.
The Manager
needs to have an array of Items
. These Items
are added to the Manager
from the Main
object.
I'd like to know how should I pass the Items
to the Manager
in order to make them live even outside the Main() function scope, but at the same time, being able to delete them when the Manager
is destroyed.
NOTE
Item
, inside Manager
, have to be a pointer because I need to check for NULL items
So far I have something like this (not actual code for short):
Main
{
Manager* Man;
Main()
{
Man = new Manager(/**/); //i use a pointer because i need this object to persist;
Item* it = new Item(/**/);
Man->AddItem(it);
}
~Main()
{
delete(Man);
}
}
Manager
{
Item* ItemArchive[15];
void AddItem(Item* item)
{
ItemArchive[index] = item;
}
~Manager()
{
for(int i=0;i<archiveLength;i++)
delete(ItemArchive[i]); //Here i get a runtime error,most probably an
//access violation,can't be more specific
//because Unreal Engine doesn't give me that info
}
}
Item
{
//just a basic object
}
So my question is, how can I create the objects in the Main
and then being able to use and delete them inside the Manager
?