From c1d0a6e436ab0868e4e3d9ea0ed4a792975e954b Mon Sep 17 00:00:00 2001 From: pezy Date: Wed, 28 Jan 2015 20:03:11 +0800 Subject: [PATCH] more better solution --- ch13/README.md | 5 ++++- ch13/ex13_34_36_37.cpp | 33 ++++++++++++++++++++++++++++++++- ch13/ex13_34_36_37.h | 8 ++++++++ 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/ch13/README.md b/ch13/README.md index b78329e2..9d2cda8e 100644 --- a/ch13/README.md +++ b/ch13/README.md @@ -220,4 +220,7 @@ some existing `Folders` will out of sync with the `Message` after assignment. >We did not use copy and swap to define the Message assignment operator. Why do you suppose this is so? @Mooophy -The copy and swap is an elegant way when working with dynamicly allocated memory. In the Message class ,noing is allocated dynamically. Thus using this idiom makes no sense and will make it more complicated to implement due to the pointers that point back. \ No newline at end of file +The copy and swap is an elegant way when working with dynamicly allocated memory. In the Message class ,noing is allocated dynamically. Thus using this idiom makes no sense and will make it more complicated to implement due to the pointers that point back. + +@pezy +In this case, `swap` function is special. It will be clear two `Message`'s folders , then swap members, and added themselves to each folders. But, `Message` assignment operator just clear itself, and copy the members, and added itself to each folders. The `rhs` don't need to clear and add to folders. So, if using copy and swap to define, it will be very inefficiency. \ No newline at end of file diff --git a/ch13/ex13_34_36_37.cpp b/ch13/ex13_34_36_37.cpp index 43185da1..fc193f27 100644 --- a/ch13/ex13_34_36_37.cpp +++ b/ch13/ex13_34_36_37.cpp @@ -1,4 +1,5 @@ -#include "ex12_34_36_37.h" +#include "ex13_34_36_37.h" +#include void swap(Message &lhs, Message &rhs) { @@ -62,8 +63,31 @@ Message& Message::operator=(const Message &rhs) return *this; } +void Message::print_debug() +{ + std::cout << contents << std::endl; +} + // Folder Implementation +void swap(Folder &lhs, Folder &rhs) +{ + using std::swap; + for (auto m : lhs.msgs) + m->remFldr(&lhs); + + for (auto m : rhs.msgs) + m->remFldr(&rhs); + + swap(lhs.msgs, rhs.msgs); + + for (auto m : lhs.msgs) + m->addFldr(&lhs); + + for (auto m : rhs.msgs) + m->addFldr(&rhs); +} + void Folder::add_to_Message(const Folder &f) { for (auto m : f.msgs) @@ -93,3 +117,10 @@ Folder& Folder::operator =(const Folder &rhs) add_to_Message(rhs); return *this; } + +void Folder::print_debug() +{ + for (auto m : msgs) + std::cout << m->contents << " "; + std::cout << std::endl; +} diff --git a/ch13/ex13_34_36_37.h b/ch13/ex13_34_36_37.h index f3c538ec..875db79e 100644 --- a/ch13/ex13_34_36_37.h +++ b/ch13/ex13_34_36_37.h @@ -22,6 +22,7 @@ class Folder; class Message { friend void swap(Message &, Message &); + friend void swap(Folder &, Folder &); friend class Folder; public: explicit Message(const std::string &str = ""):contents(str) {} @@ -31,6 +32,8 @@ class Message { void save(Folder&); void remove(Folder&); + void print_debug(); + private: std::string contents; std::set folders; @@ -46,6 +49,7 @@ void swap(Message&, Message&); class Folder { friend void swap(Message&, Message&); + friend void swap(Folder &, Folder &); friend class Message; public: Folder() = default; @@ -53,6 +57,8 @@ class Folder { Folder& operator=(const Folder &); ~Folder(); + void print_debug(); + private: std::set msgs; @@ -63,5 +69,7 @@ class Folder { void remMsg(Message *m) { msgs.erase(m); } }; +void swap(Folder &, Folder &); + #endif // MESSAGE