码迷,mamicode.com
首页 > 其他好文 > 详细

Messages的例子

时间:2014-08-22 00:12:45      阅读:331      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   os   io   for   div   amp   

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);
}

 

Messages的例子,布布扣,bubuko.com

Messages的例子

标签:style   blog   color   os   io   for   div   amp   

原文地址:http://www.cnblogs.com/wuchanming/p/3928310.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!