This is my scenario (for brevity I have taken out the permissions and instance variables, only the relevant methods):
template <typename ItemType>
class LinkedList
{
class Item
{
Item(ItemType theObj, bool freeObj);
~Item();
};
void add(LinkedList<ItemType>::Item item);
};
template <typename ItemType>
LinkedList<ItemType>::Item::~Item()
{
if(freeObj) {
delete obj;
}
}
In my code I used it like this:
// works great
LinkedList<MyObject*> mylist;
mylist.add(LinkedList<MyObject*>::Item(new MyObject(), false));
// won't compile because you cannot use delete operator on a non-pointer
LinkedList<MyObject> mylist;
mylist.add(LinkedList<MyObject>::Item(MyObject(), false));
Let's put aside the fact that LinkedList<>::Item should not free the actual object. This is more of an understanding templates question. I need to specify an explicit template for the Item destructor to handle when ItemType is a pointer and when its not.
Is this possible? What are some potential design/leak pit falls? Thanks.