//Exercises Section 13.4
//Exercise 13.33: Why is the parameter to the save and remove members
//of Message a Folder&? Why didn’t we define that parameter as Folder?
//Or const Folder&?
//Because it has changed the f;
//Exercise 13.35: What would happen if Message used the synthesized
//versions of the copy-control members?
//the copy will be a mass.
//Exercise 13.38: We did not use copy and swap to define the Message
//assignment operator. Why do you suppose this is so?
//It is not efficient. If use the swap. The rhs and lhs will be clear folders and then swap. But assignment just need clear one.
#include <string>
#include <set>
using namespace std;
class Folder;
class Message {
friend void swap(Message &, Message &);
friend class Folder;
public:
explicit Message(const string &s = "") : contents(s) { }
Message(const Message &);
Message &operator=(const Message &);
~Message();
void save(Folder &);
void remove(Folder &);
void print_debug();
private:
string contents;
set<Folder *> folders;
void add_to_Folders(const Message &);
void remove_from_Folders();
void addFldr(Folder *f) { folders.insert(f); }
void remFldr(Folder *f) { folders.erase(f); }
};
void swap(Message &, Message &);
class Folder {
friend void swap(Folder &, Folder &);
friend class Message;
public:
Folder() = default;
Folder(const Folder &);
Folder &operator=(const Folder &);
~Folder();
void print_debug();
private:
set<Message *> msgs;
void add_to_Message(const Folder&);
void remove_from_Message();
void addMsg(Message *m) { msgs.insert(m); }
void remMsg(Message *m) { msgs.erase(m); }
};
void swap(Folder &, Folder &);
#include "ChenHC.h"
#include <iostream>
#include <set>
#include <string>
using namespace std;
void swap(Message &lhs, Message &rhs)
{
using std::swap;
lhs.remove_from_Folders();
rhs.remove_from_Folders();
swap(lhs.folders, rhs.folders);
swap(lhs.contents, rhs.contents);
lhs.add_to_Folders(lhs);
rhs.add_to_Folders(rhs);
}
void Message::save(Folder &f)
{
addFldr(&f);
f.addMsg(this);
}
void Message::add_to_Folders(const Message &m)
{
for (auto f : m.folders) f->addMsg(this);
}
Message::Message(const Message &m) : contents(m.contents), folders(m.folders)
{
add_to_Folders(m);
}
void Message::remove_from_Folders()
{
for (auto f : folders) f->remMsg(this);
}
Message::~Message()
{
remove_from_Folders();
}
Message &Message::operator=(const Message &rhs)
{
remove_from_Folders();
contents = rhs.contents;
folders = rhs.folders;
add_to_Folders(rhs);
return *this;
}
void Message::print_debug()
{
cout << contents << endl;
}
void swap(Folder &lhs, Folder &rhs)
{
using std::swap;
lhs.remove_from_Message();
rhs.remove_from_Message();
swap(lhs.msgs, rhs.msgs);
lhs.add_to_Message(lhs);
rhs.add_to_Message(rhs);
}
void Folder::add_to_Message(const Folder &f)
{
for (auto m : f.msgs) m->addFldr(this);
}
Folder::Folder(const Folder &f) : msgs(f.msgs)
{
add_to_Message(f);
}
void Folder::remove_from_Message()
{
for (auto m : msgs) m->remFldr(this);
}
Folder::~Folder()
{
remove_from_Message();
}
Folder &Folder::operator=(const Folder &rhs)
{
remove_from_Message();
msgs = rhs.msgs;
add_to_Message(rhs);
return *this;
}
void Folder::print_debug()
{
for (auto m : msgs) cout << m->contents << " ";
cout << endl;
}
int main() {
return 0;
}
13.4. A Copy-Control Example
最新推荐文章于 2024-11-04 16:58:15 发布