c++ primer(第五版)笔记 第十三章(3) 拷贝控制

31 篇文章 0 订阅
//folder.h
#ifndef __FOLDER_H
#define __FOLDER_H

#include<set>
class Message;
class Folder{
	friend class Message;
	friend void swap( Message&, Message&);
public:
	Folder(){} 
	Folder( const Folder&);
	Folder& operator=( const Folder&);
	~Folder();
	
	void save(Message&);
	void remove(Message&);
	void debug_print();
private:
	std::set< Message*> msgs;
	void add_to_msgs( const Folder&);
	void remove_from_msgs();
	void rm_a_msg( Message *m){
		msgs.erase( m);
	}
	void add_a_msg( Message *m){
		msgs.insert( m);
	}
};
void swap( Message&, Message&);

#endif //__FOLDER_H

//<span style="font-family: Arial, Helvetica, sans-serif;">folder.cc</span>
#include "message.h"
#include "folder.h"
#include<iostream>

void Folder::add_to_msgs( const Folder& f){
	for( auto m : f.msgs)
		m->add_a_folder( this);
}

void Folder::remove_from_msgs(){
	for( auto m : msgs)
		m->rm_a_folder( this);
}


Folder::Folder( const Folder &f) :
	msgs(f.msgs){
	add_to_msgs( f);
}
Folder& Folder::operator=( const Folder& f){
	remove_from_msgs();
	msgs = f.msgs;
	add_to_msgs( f);
	return *this;
}

Folder::~Folder(){
	remove_from_msgs();
}


void Folder::save( Message &m){
	add_a_msg( &m);
	m.add_a_folder( this);
}

void Folder::remove( Message &m){
	rm_a_msg( &m);
	m.rm_a_folder( this);
}

void Folder::debug_print()
{
    std::cerr << "Folder contains " << msgs.size() << " messages" << std::endl;
	for( auto m : msgs)
		std::cerr << "Message : " << m->contents << std::endl;
}

//message.h
#ifndef __MESSAGE_H
#define __MESSAGE_H

#include<string>
#include<set>
class Folder;
class Message{
	friend class Folder;
	friend void swap( Message &, Message &);
public:
	explicit Message( const std::string &str = "") :
		contents( str) {}
	Message( const Message &);
	Message& operator=( const Message &);
	~Message();
	
	void save( Folder&);
	void remove( Folder&);
	void debug_print();
private:
	std::string contents;
	std::set< Folder*> folders;
	void add_to_folders( const Message &);
	void remove_from_Folders();
	void add_a_folder( Folder *f){ 
		folders.insert( f);
	}
	void rm_a_folder( Folder *f){ 
		folders.erase( f);
	}
};
void swap( Message &, Message &);

#endif //__MESSAGE_H

//message.cc
#include "message.h"
#include "folder.h"
#include<iostream>

void Message::add_to_folders( const Message &m){
	for( auto f : m.folders)
		f->add_a_msg( this);
}

void Message::remove_from_Folders(){
	for( auto f : folders)
		f->rm_a_msg( this);
	folders.clear();
}

Message::Message( const Message &m) : 
	contents( m.contents), folders( m.folders){
	add_to_folders( m);
}
Message& Message::operator=( const Message &m){
	remove_from_Folders();
	contents = m.contents;
	folders = m.folders;
	add_to_folders( m);
	return *this;
}
Message::~Message(){
	remove_from_Folders();
}

void Message::save( Folder &f){
	folders.insert( &f);
	f.add_a_msg( this);
}
void Message::remove( Folder &f){
	folders.erase( &f);
	f.rm_a_msg( this);
}

void swap( Message &lm, Message &rm){
	using std::swap;
	for( auto f : ( lm.folders))
		f->rm_a_msg( &lm);
	for( auto f : ( rm.folders))
		f->rm_a_msg( &rm);
	swap( lm.folders, rm.folders);
	swap( lm.contents, rm.contents);
	for( auto f : ( lm.folders))
		f->add_a_msg( &lm);
	for( auto f : ( rm.folders))
		f->add_a_msg( &rm);
}
void Message::debug_print()
{
    std::cerr << "Message:\n\t" << contents << std::endl;
    std::cerr << "Appears in " << folders.size() << " Folders" << std::endl;
}


//testmain.cc
/*
 * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
 * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
 * copyright and warranty notices given in that book:
 * 
 * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
 * 
 * 
 * "The authors and publisher have taken care in the preparation of this book,
 * but make no expressed or implied warranty of any kind and assume no
 * responsibility for errors or omissions. No liability is assumed for
 * incidental or consequential damages in connection with or arising out of the
 * use of the information or programs contained herein."
 * 
 * Permission is granted for this code to be used for educational purposes in
 * association with the book, given proper citation if and when posted or
 * reproduced.Any commercial use of this code requires the explicit written
 * permission of the publisher, Addison-Wesley Professional, a division of
 * Pearson Education, Inc. Send your request for permission, stating clearly
 * what code you would like to use, and in what specific way, to the following
 * address: 
 * 
 * 	Pearson Education, Inc.
 * 	Rights and Permissions Department
 * 	One Lake Street
 * 	Upper Saddle River, NJ  07458
 * 	Fax: (201) 236-3290
*/ 

#include <vector>
using std::vector;

#include <string>
using std::string; 

#include <iostream>
using std::cout; using std::endl;

#include "message.h"
#include "folder.h"

int main()
{
	string s1("contents1");
	string s2("contents2");
	string s3("contents3");
	string s4("contents4");
	string s5("contents5");
	string s6("contents6");
	
	// all new messages, no copies yet
	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();

	// create some copies
	Message c1(m1);
	Message c2(m2), c4(m4), c6(m6);
	
	m1.debug_print();
	f2.debug_print();

	// now some assignments
	m2 = m3;
	m4 = m5;
	m6 = m3;
	m1 = m5;

	m1.debug_print();
	f2.debug_print();

	// finally, self-assignment
	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 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值