The Message Destructor

When a Message is destroyed, we must remove this Message from the Folders 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 ...

Get C++ Primer, Fifth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.