zoukankan      html  css  js  c++  java
  • Messages的例子

    13.33 13.36 13.37

    Message.h

    #ifndef MESSAGE_H
    #define MESSAGE_H
    #include<iostream>
    #include<string>
    #include<set>
    #include"Folder.h"
    using namespace std;
    class Folder;
    class Message
    {
    friend class Folder;
    public:
        explicit Message(const string &str=""):contents(str) {}
        Message(const Message&);
        Message& operator=(const Message&);
        ~Message();
        void save(Folder&);
        void remove(Folder&);
        void addFolder(Folder*);
        void remFolder(Folder*);
        void swap(Message&,Message&);
    private:
        string contents;
        set<Folder*> folders;
        void add_to_Folders(const Message&);
        void remove_to_Folders();
    };
    
    #endif

    Message.cpp

    #include"Message.h"
    
    void Message::save(Folder &f)
    {
        folders.insert(&f);
        f.addMsg(this);
    }
    
    void Message::remove(Folder &f)
    {
        folders.erase(&f);
        f.remMsg(this);
    }
    
    void Message::add_to_Folders(const Message& m)
    {
        for(auto f:m.folders)
            f->addMsg(this);
    }
    
    void Message::remove_to_Folders()
    {
        for(auto f:folders)
            f->remMsg(this);
    }
    
    Message::Message(const Message &m):contents(m.contents),folders(m.folders)
    {
        add_to_Folders(m);
    }
    
    Message& Message::operator=(const Message& m)
    {
        remove_to_Folders();
        contents=m.contents;
        folders=m.folders;
        add_to_Folders(m);
        return *this;
    }
    
    Message::~Message()
    {
        remove_to_Folders();
    }
    
    void Message::addFolder(Folder* f)
    {
        folders.insert(f);
    }
    
    void Message::remFolder(Folder* f)
    {
        folders.erase(f);
    }
    void Message::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);
    }

    Folder.h

    #ifndef FOLDER_H
    #define FOLDER_H
    #include<iostream>
    #include<set>
    #include<string>
    using namespace std;
    class Message;
    class Folder
    {
    friend class Message;
    private:
        set<Message*> msg;
    public:
        void addMsg(Message*);
        void remMsg(Message*);
    };
    #endif // FOLDER_H

    Folder.cpp

    #include"Folder.h"
    #include"Message.h"
    void Folder::addMsg(Message *m)
    {
        msg.insert(m);
    }
    
    void Folder::remMsg(Message *m)
    {
        msg.erase(m);
    }
  • 相关阅读:
    EL表达式与JSTL
    jsp
    Servlet 会话
    Servlet 常用类
    Servlet
    Java 网络编程
    CentOS系统下安装python3+Django
    转载Alpine Linux常用命令
    转载Alpine基础
    CentOS启动docker1.13失败(Job for docker.service failed because the control process exited with error code. See "systemctl status docker.service" and "journalctl -xe" for details.)
  • 原文地址:https://www.cnblogs.com/wuchanming/p/3928310.html
Copyright © 2011-2022 走看看