I have nearly finished an assignment that calls for implementing a media library. I have pinpointed my current solution for the addItem method in the library class as a memory leak, but so far I am not certain how to fix it. The addItem method I have so far is as follows (addBook shown; Book is a subclass of Item):
const Item* Library::addBook(const string& title, const string& author, const int nPages)
{
Item* book = new Book(title, author, nPages);
lib->insert(book); //lib is a set<Item*> container
return book;
}
I understand that if I use new to allocate an instance of Item, I need to delete it. Of course, I can't delete it before the return statement, nor after. The addBook definition is part of the assignment and can't be changed.