Message
ClassGiven this design, we can write our Message
class as follows:
class Message {
friend class Folder;
public:
// folders is implicitly initialized to the empty set
explicit Message(const std::string &str = ""):
contents(str) { }
// copy control to manage pointers to this Message
Message(const Message&); // copy constructor
Message& operator=(const Message&); // copy assignment
~Message(); // destructor
// add/remove this Message from the specified Folder's set of messages
void save(Folder&);
void remove(Folder&);
private:
std::string contents; // actual message text
std::set<Folder*> folders; // Folders that have this Message
// utility functions used by copy constructor, assignment, and destructor
// add this Message to the Folders that point to the parameter
void add_to_Folders(const Message&);
// remove this Message from every Folder in folders
void remove_from_Folders();
};
The class defines two data members: contents
, to store the message text, and folders
, to store pointers to the Folder
s in which this Message
appears. The constructor that takes a string
copies the given string
into contents
and (implicitly) initializes folders
to the empty set. Because this constructor has a default argument, it is also the Message
default constructor (§ 7.5.1, p. 290).