【C++ Primer 第十三章】4. 拷贝控制示例

时间:2023-03-08 16:10:48

拷贝控制示例

 #include<iostream>
#include<string>
#include<set>
#include<vector>
using namespace std; class Folder; class Message {
friend void swap(Message&, Message&);
friend class Folder;
public:
explicit Message(const string &str = "") : contents(str) {}
Message(const Message&);
Message& operator=(const Message&);
~Message(); Message(Message&&);
Message& operator=(Message&&); void save(Folder&);
void remove(Folder&);
void debug_print(); 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 move_Folders(Message*);
}; /*------------------------------------------------------------------------------------------------------*/ class Folder {
friend void swap(Message&, Message&);
friend class Message;
public:
Folder() = default;
Folder(const Folder&);
Folder& operator=(const Folder&); Folder(Folder&&);
Folder& operator=(Folder&&);
~Folder(); void save(Message&);
void remove(Message&);
void debug_print(); private:
set<Message*> msgs; void add_to_Messages(const Folder&);
void remove_from_Msgs();
void addMsg(Message *m) { msgs.insert(m); }
void remMsg(Message *m) { msgs.erase(m); }
void move_Messages(Folder*); }; /*---------------------------------------Folder类成员函数--------------------------------------------------*/ Folder::Folder(const Folder &f) : msgs(f.msgs)
{
add_to_Messages(f);
} void Folder::add_to_Messages(const Folder &f)
{
for (auto msg : f.msgs)
msg->addFldr(this);
} Folder& Folder::operator=(const Folder &f)
{
remove_from_Msgs();
msgs = f.msgs;
add_to_Messages(f);
return *this;
} void Folder::remove_from_Msgs()
{
while (!msgs.empty())
(*msgs.begin())->remove(*this); // Message类成员函数
} Folder::~Folder()
{
remove_from_Msgs();
} Folder::Folder(Folder &&f)
{
move_Messages(&f);
} void Folder::move_Messages(Folder *f)
{
msgs = std::move(f->msgs);
f->msgs.clear();
for (auto m : msgs)
{
m->remFldr(f);
m->addFldr(this);
}
} Folder& Folder::operator=(Folder &&f)
{
if (this != &f)
{
remove_from_Msgs();
move_Messages(&f);
}
return *this;
} void Folder::save(Message &m)
{
msgs.insert(&m);
m.addFldr(this);
} void Folder::remove(Message &m)
{
msgs.erase(&m);
m.remFldr(this);
} void Folder::debug_print()
{
cerr << "Folder contains " << msgs.size() << " messages" << endl;
int ctr = ;
for (auto m : msgs)
{
cerr << "Message " << ctr++ << ":\n\t" << m->contents << endl;
}
} /*---------------------------------------Message类成员函数------------------------------------------------*/ Message::Message(const Message &m) : contents(m.contents), folders(m.folders)
{
add_to_Folders(m);
} void Message::add_to_Folders(const Message &m)
{
for (auto f : m.folders)
f->addMsg(this); // Folder类成员函数
} Message& Message::operator=(const Message &rhs)
{
remove_from_Folders();
contents = rhs.contents;
folders = rhs.folders;
add_to_Folders(rhs);
return *this;
} void Message::remove_from_Folders()
{
for (auto f : folders)
f->remMsg(this);
folders.clear();
} Message::Message(Message &&m) : contents(std::move(m.contents))
{
move_Folders(&m);
} void Message::move_Folders(Message *m)
{
folders = std::move(m->folders);
for (auto f : folders)
{
f->remMsg(m);
f->addMsg(this);
}
m->folders.clear();
} Message& Message::operator=(Message &&rhs)
{
if (this != &rhs)
{
remove_from_Folders();
contents = std::move(rhs.contents);
move_Folders(&rhs);
}
return *this;
} Message::~Message()
{
remove_from_Folders();
} void Message::save(Folder &f)
{
folders.insert(&f);
f.addMsg(this);
} void Message::remove(Folder &f)
{
folders.erase(&f);
f.remMsg(this);
} void Message::debug_print()
{
cerr << "Message:\n\t" << contents << endl;
cerr << "Appears in " << folders.size() << " Folders" << endl;
} void swap(Message &lhs, Message &rhs)
{
using std::swap;
for (auto f : lhs.folders)
f->remMsg(&lhs);
for (auto f : rhs.folders)
f->remMsg(&rhs);
swap(lhs.folders, rhs.folders);
swap(lhs.contents, rhs.contents);
for (auto f : lhs.folders)
f->addMsg(&lhs);
for (auto f : rhs.folders)
f->addMsg(&rhs);
} /*-----------------------------------------------------------------------------------------------------*/ int main()
{
string s1("contents1");
string s2("contents2");
string s3("contents3");
string s4("contents4");
string s5("contents5");
string s6("contents6");
Message m1(s1);
Message m2(s2);
Message m3(s3);
Message m4(s4);
Message m5(s5);
Message m6(s6); Folder f1;
Folder f2; m1.save(f1); m3.save(f1); m5.save(f1);
m1.save(f2); m2.save(f2); m4.save(f2);
m6.save(f2); m1.debug_print();
f2.debug_print(); Message c1(m1);
Message c2(m2), c4(m4), c6(m6); m1.debug_print();
f2.debug_print(); m2 = m3;
m4 = m5;
m6 = m3;
m1 = m5; m1.debug_print();
f2.debug_print(); m2 = m2;
m1 = m1; m1.debug_print();
f2.debug_print(); vector<Message> vm;
cout << "capacity: " << vm.capacity() << endl;
vm.push_back(m1); cout << "capacity: " << vm.capacity() << endl;
vm.push_back(m2); cout << "capacity: " << vm.capacity() << endl;
vm.push_back(m3); cout << "capacity: " << vm.capacity() << endl;
vm.push_back(m4); cout << "capacity: " << vm.capacity() << endl;
vm.push_back(m5); cout << "capacity: " << vm.capacity() << endl;
vm.push_back(m6); vector<Folder> vf;
cout << "capacity: " << vf.capacity() << endl;
vf.push_back(f1); cout << "capacity: " << vf.capacity() << endl;
vf.push_back(f2); cout << "capacity: " << vf.capacity() << endl;
vf.push_back(Folder(f1)); cout << "capacity: " << vf.capacity() << endl;
vf.push_back(Folder(f2)); cout << "capacity: " << vf.capacity() << endl;
vf.push_back(Folder()); Folder f3;
f3.save(m6);
cout << "capacity: " << vf.capacity() << endl;
vf.push_back(f3);
return ;
}

运行结果:

Message:
contents1
Appears in Folders
Folder contains messages
Message :
contents6
Message :
contents4
Message :
contents2
Message :
contents1
Message:
contents1
Appears in Folders
Folder contains messages
Message :
contents6
Message :
contents4
Message :
contents2
Message :
contents1
Message :
contents6
Message :
contents4
Message :
contents2
Message :
contents1
Message:
contents5
Appears in Folders
Folder contains messages
Message :
contents6
Message :
contents4
Message :
contents2
Message :
contents1
Message:
contents5
Appears in Folders
Folder contains messages
Message :
contents6
Message :
contents4
Message :
contents2
Message :
contents1
capacity:
capacity:
capacity:
capacity:
capacity:
capacity:
capacity:
capacity:
capacity:
capacity:
capacity:
capacity:
请按任意键继续. . .