Hi! I am sorry if question is too newbish, I just want to settle this question for myself once and for all, to make sure I never do mistakes in the future.
If the first scenario is ok (no malloc), then how can I tell, when something creates variable copy and manages its memory? Only by looking at the tarray source code?
If second scenario is ok (with malloc), then should I do it and forget after adding to tarray - i.e. tarray will manage memory for this pointer from now on, and will deallocate later on by itself too?
This concept is basically each part of your code should focus its own needs.
Instead of doing all this work in one function
Make the function only worry about its own needs
void MyClass::AddToInventory(FInventoryItem item)
{
item.UnEquip(); // Telling the item it is not Equipped means this function needs to know less about the item
this->equipment.Add(item);
}
In the part of your code that is Calling AddToInventory it can decide what item to create and add to the inventory.
Struct vs Class
If your item has behavior associated with it its normally better to make it a class. In your case the basic behavior would be something like
void InventoryItem::UnEquip()
{
this.isEquipped = false;
}
InventoryItem::Equip()
{
this.isEquipped = true;
// TODO: track where and who this item is equipped.
}
For a complete working example check out this inventory Tutorial if you have not seen it already