13.4. A Copy-Control Example

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值