Message
ClassWhen we copy a Message
, the copy should appear in the same Folder
s as the original Message
. As a result, we must traverse the set
of Folder
pointers adding a pointer to the new Message
to each Folder
that points to the original Message
. Both the copy constructor and the copy-assignment operator will need to do this work, so we’ll define a function to do this common processing:
// add this Message to Folders that point to m
void Message::add_to_Folders(const Message &m)
{
for (auto f : m.folders) // for each Folder that holds m
f->addMsg(this); // add a pointer to this Message to that Folder
}
Here we call addMsg
on each Folder
in m.folders
. The addMsg
function will add a pointer to this Message
to that Folder
.
The Message
copy constructor copies the data members of the given object:
Message::Message(const Message &m):
contents(m.contents), folders(m.folders)
{
add_to_Folders(m); // add this Message to the Folders that point to m
}
and calls add_to_Folders
to add a pointer to the newly created Message
to each Folder
that contains the original Message
.