Catalog Catalog::search (const string& s)
{
Catalog result;
... inside a loop running through the linked list:
if ((current->entry.title.find (s) != string::npos) ||
(current->entry.author.find (s) != string::npos)
)
result += current->entry;
... end of loop
return result;
}
Catalog catalog;
Catalog results;
Catalog savedResults;
...
savedResults = results;
results = catalog.search (inputString);
...
savedResults = results;
results = results.search (inputString);
...
results = savedResults;
Program the assignment operator to make a real (“deep”) copy of the linked list of catalog nodes. That takes a loop to run through the original that is being copied. The copy constructor then becomes easy because it can use the assignment operator ...
// copy constructor
Catalog::Catalog (const Catalog& cat)
{
head = NULL;
*this = cat; // piggy-backing on assignment operator
}
So we need the three things you always need when doing dynamic memory allocation (i.e., when you use “new ... ”, whether you are allocating an array or a node pointed to by pointers):
© 2002 Karl Winklmann