回复 2楼 wp231957
额。Folder是一个类,folder是Message的一个成员变量
#ifndef FOLDER_H #define FOLDER_H #include<set> #include"Message.h" class Folder{ public: void addMsg(Message *); void remMsg(Message *); private: std::set<Message*> message; }; void Folder::addMsg(Message *a) { message.insert(a); } void Folder::remMsg(Message *a) { message.erase(a); } #endif
#ifndef MESSAGE_H #define MESSAGE_H #include<string> #include<set> #include"Folder.h" using std::string; class Message{ public: Message() = default; Message(string a) :content(a) { }; Message(const Message&); Message& operator=(const Message&); ~Message(); void swap(Message&, Message&); void save(Folder&); void remove(Folder&); void add_to_folder(const Message&); void rem_to_folder(); private: string content; std::set<Folder*> folder; }; Message::Message(const Message& a) { content = a.content; folder = a.folder; add_to_folder(a); } Message& Message::operator = (const Message& a) { rem_to_folder(); content = a.content; folder = a.folder; add_to_folder(a); return *this; } void Message::add_to_folder(const Message& a) { for (auto use : a.folder) { use->addMsg(this); } } void Message::rem_to_folder() { for (auto use : folder) { use->remMsg(this); } } Message::~Message() { rem_to_folder(); } void Message::swap(Message &a, Message &b) { Message mid; } void Message::save(Folder &a) { a.addMsg(this); folder.insert(&a); } void Message::remove(Folder &a) { a.remMsg(this); folder.erase(&a); } #endif