六一的部落格


关关难过关关过,前路漫漫亦灿灿。



使用文件夹管理消息: 文件夹中保存多个消息; 消息可以存放在多个文件夹中


文件夹类

 1class Message;
 2
 3class Folder
 4{
 5public:
 6    void addMsg(Message *);
 7    void remMsg(Message *);
 8    void print();
 9
10private:
11    set<Message *> messages;
12};
 1void Folder::addMsg(Message *m)
 2{
 3    messages.insert(m);
 4}
 5
 6void Folder::remMsg(Message *m)
 7{
 8    messages.erase(m);
 9}
10
11void Folder::print()
12{
13    for (auto msg : messages)
14    {
15        msg->print();
16        cout << endl;
17    }
18}

消息类

 1class Folder;
 2
 3class Message
 4{
 5    friend class Folder;
 6    friend void swap(Message &, Message &);
 7
 8public:
 9    explicit Message(const string &str = "") : contents(str) {} // 显式默认构造函数,也是显式转换构造函数
10    Message(const Message &);                                   // 拷贝构造函数
11    Message &operator=(const Message &);                        // 拷贝赋值运算符
12    ~Message();                                                 // 析构函数
13    Message(Message &&);
14    Message &operator=(Message &&);
15
16    void save(Folder &);
17    void remove(Folder &);
18    void print() { cout << contents; }
19
20private:
21    string contents;
22    set<Folder *> folders;
23    void add_to_Folders(const Message &);
24    void remove_from_Folders();
25    void move_Folders(Message *);
26};
 1Message::Message(const Message &m) : contents(m.contents), folders(m.folders)
 2{
 3    add_to_Folders(m);
 4}
 5
 6Message &Message::operator=(const Message &rhs)
 7{
 8    remove_from_Folders();
 9
10    contents = rhs.contents;
11    folders = rhs.folders;
12
13    add_to_Folders(rhs);
14
15    return *this;
16}
17
18Message::~Message()
19{
20    remove_from_Folders();
21}
22
23Message::Message(Message &&m) : contents(std::move(m.contents))
24{
25    move_Folders(&m);
26}
27
28Message &Message::operator=(Message &&rhs)
29{
30    if (this != &rhs)
31    {
32        remove_from_Folders();
33        contents = std::move(rhs.contents);
34        move_Folders(&rhs);
35    }
36    return *this;
37}
38
39void Message::save(Folder &f)
40{
41    folders.insert(&f);
42    f.addMsg(this);
43}
44
45void Message::remove(Folder &f)
46{
47    folders.erase(&f);
48    f.remMsg(this);
49}
50
51void Message::add_to_Folders(const Message &m)
52{
53    for (auto f : m.folders)
54        f->addMsg(this);
55}
56
57void Message::remove_from_Folders()
58{
59    for (auto f : folders)
60        f->remMsg(this);
61}
62
63void Message::move_Folders(Message *m)
64{
65    folders = std::move(m->folders);
66    for (auto f : folders)
67    {
68        f->remMsg(m);
69        f->addMsg(this);
70    }
71    m->folders.clear();
72}
73
74inline void swap(Message &lhs, Message &rhs)
75{
76    lhs.remove_from_Folders();
77
78    rhs.remove_from_Folders();
79
80    swap(lhs.contents, rhs.contents);
81    swap(lhs.folders, rhs.folders);
82
83    rhs.add_to_Folders(rhs);
84}

测试

手动创建消息和文件夹

 1#include <iostream>
 2#include <string>
 3#include <set>
 4
 5using namespace std;
 6
 7// Message and Folder
 8
 9int main()
10{
11    Folder f1;
12    Folder f2;
13
14    Message m1("Hello!");
15    m1.save(f1);                    // 将"Hello!"加到f1
16    m1.save(f2);                    // 将"Hello!"加到f2
17
18    Message m2 = m1;                // 将"Hello!"加到f1和f2
19
20    Message m3("How are you?");
21    m3.save(f1);                    // 将"How are you?"加到f1
22
23    f1.print();
24    cout << endl;
25
26    f2.print();
27    cout << endl;
28
29    return 0;
30}

输出

How are you?
Hello!
Hello!

Hello!
Hello!

拷贝控制: 使用文件夹管理消息


使用文件夹管理消息: 文件夹中保存多个消息; 消息可以存放在多个文件夹中


文件夹类

 1class Message;
 2
 3class Folder
 4{
 5public:
 6    void addMsg(Message *);
 7    void remMsg(Message *);
 8    void print();
 9
10private:
11    set<Message *> messages;
12};
 1void Folder::addMsg(Message *m)
 2{
 3    messages.insert(m);
 4}
 5
 6void Folder::remMsg(Message *m)
 7{
 8    messages.erase(m);
 9}
10
11void Folder::print()
12{
13    for (auto msg : messages)
14    {
15        msg->print();
16        cout << endl;
17    }
18}

消息类

 1class Folder;
 2
 3class Message
 4{
 5    friend class Folder;
 6    friend void swap(Message &, Message &);
 7
 8public:
 9    explicit Message(const string &str = "") : contents(str) {} // 显式默认构造函数,也是显式转换构造函数
10    Message(const Message &);                                   // 拷贝构造函数
11    Message &operator=(const Message &);                        // 拷贝赋值运算符
12    ~Message();                                                 // 析构函数
13    Message(Message &&);
14    Message &operator=(Message &&);
15
16    void save(Folder &);
17    void remove(Folder &);
18    void print() { cout << contents; }
19
20private:
21    string contents;
22    set<Folder *> folders;
23    void add_to_Folders(const Message &);
24    void remove_from_Folders();
25    void move_Folders(Message *);
26};
 1Message::Message(const Message &m) : contents(m.contents), folders(m.folders)
 2{
 3    add_to_Folders(m);
 4}
 5
 6Message &Message::operator=(const Message &rhs)
 7{
 8    remove_from_Folders();
 9
10    contents = rhs.contents;
11    folders = rhs.folders;
12
13    add_to_Folders(rhs);
14
15    return *this;
16}
17
18Message::~Message()
19{
20    remove_from_Folders();
21}
22
23Message::Message(Message &&m) : contents(std::move(m.contents))
24{
25    move_Folders(&m);
26}
27
28Message &Message::operator=(Message &&rhs)
29{
30    if (this != &rhs)
31    {
32        remove_from_Folders();
33        contents = std::move(rhs.contents);
34        move_Folders(&rhs);
35    }
36    return *this;
37}
38
39void Message::save(Folder &f)
40{
41    folders.insert(&f);
42    f.addMsg(this);
43}
44
45void Message::remove(Folder &f)
46{
47    folders.erase(&f);
48    f.remMsg(this);
49}
50
51void Message::add_to_Folders(const Message &m)
52{
53    for (auto f : m.folders)
54        f->addMsg(this);
55}
56
57void Message::remove_from_Folders()
58{
59    for (auto f : folders)
60        f->remMsg(this);
61}
62
63void Message::move_Folders(Message *m)
64{
65    folders = std::move(m->folders);
66    for (auto f : folders)
67    {
68        f->remMsg(m);
69        f->addMsg(this);
70    }
71    m->folders.clear();
72}
73
74inline void swap(Message &lhs, Message &rhs)
75{
76    lhs.remove_from_Folders();
77
78    rhs.remove_from_Folders();
79
80    swap(lhs.contents, rhs.contents);
81    swap(lhs.folders, rhs.folders);
82
83    rhs.add_to_Folders(rhs);
84}

测试

手动创建消息和文件夹

 1#include <iostream>
 2#include <string>
 3#include <set>
 4
 5using namespace std;
 6
 7// Message and Folder
 8
 9int main()
10{
11    Folder f1;
12    Folder f2;
13
14    Message m1("Hello!");
15    m1.save(f1);                    // 将"Hello!"加到f1
16    m1.save(f2);                    // 将"Hello!"加到f2
17
18    Message m2 = m1;                // 将"Hello!"加到f1和f2
19
20    Message m3("How are you?");
21    m3.save(f1);                    // 将"How are you?"加到f1
22
23    f1.print();
24    cout << endl;
25
26    f2.print();
27    cout << endl;
28
29    return 0;
30}

输出

How are you?
Hello!
Hello!

Hello!
Hello!