Message
DestructorWhen a Message
is destroyed, we must remove this Message
from the Folder
s that point to it. This work is shared with the copy-assignment operator, so we’ll define a common function to do it:
void Message::remove_from_Folders()
{
for (auto f : folders) // for each pointer in folders
f->remMsg(this); // remove this Message from that Folder
folders.clear(); // no Folder points to this Message
}
The implementation of the remove_from_Folders
function is similar to that of add_to_Folders
, except that it uses remMsg
to remove the current Message
.
Given the remove_from_Folders
function, writing the destructor is trivial:
Message::~Message()
{
remove_from_Folders();
}
The call to remove_from_Folders
ensures that no Folder
has a pointer to the Message
we are destroying. The compiler automatically invokes the string
destructor to free contents
and the set
destructor to clean up the memory used by those members.