#include
#include
using namespace std;
class Folder;
class Message {
public:
Message(const string &str="") :contents(str) { }
Message(const Message& msg);
Message& operator=(const Message& msg);
void save(Folder &folder);
void remove(Folder &folder);
~Message();
private:
string contents;
set folders;
void put_msg_to_folders(const set &folder);
void remove_msg_from_folders();
public:
void addFldr(Folder *folder);
void remFldr(Folder *folder);
};
class Folder {
public:
Folder() {}
Folder(const Folder& folder);
Folder& operator=(const Folder& folder);
~Folder();
void save(Message &msg);
void remove(Message &msg);
void addMsg(Message* msg);
void remMsg(Message* msg);
private:
set messages;
void put_Fldr_in_Messages(const set& msgs);
void remove_Fldr_from_Messages();
};
Message::Message(const Message& msg):contents(msg.contents),folders(msg.folders)
{
put_msg_to_folders(folders);
}
Message& Message::operator=(const Message& msg)
{
if(&msg != this) {
contents=msg.contents;
remove_msg_from_folders();
folders=msg.folders;
put_msg_to_folders(folders);
} else
return *this;
}
void Message::put_msg_to_folders(const set &folder)
{
for(set::const_iterator iter=folder.begin(); iter!=folder.end(); iter++)
(*iter)->addMsg(this);
}
void Message::remove_msg_from_folders()
{
for(set::const_iterator iter=folders.begin(); iter!=folders.end(); iter++)
(*iter)->remMsg(this);
}
Message::~Message()
{
remove_msg_from_folders();
}
void Message::addFldr(Folder * folder)
{
folders.insert(folder);
}
void Message::remFldr(Folder *folder)
{
folders.erase(folder);
}
void Message::save(Folder &folder)
{
addFldr(&folder); // folders.insert(&folder);
folder.addMsg(this);
}
void Message::remove(Folder &folder)
{
remFldr(&folder); // fodlers.erase(&folder);
folder.remMsg(this);
}
//////////////////////////////////////////////////////
//////////////////////////////////////////////////////
//////////////////////////////////////////////////////
Folder::Folder(const Folder &folder):messages(folder.messages)
{
put_Fldr_in_Messages(messages);
}
Folder& Folder::operator=(const Folder &folder)
{
if(&folder != this) {
remove_Fldr_from_Messages();
messages=folder.messages;
put_Fldr_in_Messages(messages);
}
return (*this);
}
Folder::~Folder()
{
remove_Fldr_from_Messages();
}
void Folder::save(Message &msg)
{
addMsg(&msg); // messages.insert(&msg);
msg.addFldr(this);
}
void Folder::remove(Message &msg)
{
remMsg(&msg); // messages.erase(&msg);
msg.remFldr(this);
}
void Folder::addMsg(Message *msg)
{
messages.insert(msg);
}
void Folder::remMsg(Message *msg)
{
messages.erase(msg);
}
void Folder::put_Fldr_in_Messages(const set& msgs)
{
for(set::const_iterator iter=msgs.begin(); iter !=msgs.end(); iter++)
(*iter)->addFldr(this);
}
void Folder::remove_Fldr_from_Messages()
{
for(set::iterator iter=messages.begin(); iter!=messages.end(); iter++)
(*iter)->remFldr(this);
}
int main()
{
getchar();
return 0;
}
阅读(177) | 评论(0) | 转发(0) |