Skip to content

Commit

Permalink
more better solution
Browse files Browse the repository at this point in the history
  • Loading branch information
pezy committed Jan 28, 2015
1 parent 31b2b3a commit c1d0a6e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
5 changes: 4 additions & 1 deletion ch13/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
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.
33 changes: 32 additions & 1 deletion ch13/ex13_34_36_37.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "ex12_34_36_37.h"
#include "ex13_34_36_37.h"
#include <iostream>

void swap(Message &lhs, Message &rhs)
{
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}
8 changes: 8 additions & 0 deletions ch13/ex13_34_36_37.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}
Expand All @@ -31,6 +32,8 @@ class Message {
void save(Folder&);
void remove(Folder&);

void print_debug();

private:
std::string contents;
std::set<Folder*> folders;
Expand All @@ -46,13 +49,16 @@ void swap(Message&, Message&);

class Folder {
friend void swap(Message&, Message&);
friend void swap(Folder &, Folder &);
friend class Message;
public:
Folder() = default;
Folder(const Folder &);
Folder& operator=(const Folder &);
~Folder();

void print_debug();

private:
std::set<Message*> msgs;

Expand All @@ -63,5 +69,7 @@ class Folder {
void remMsg(Message *m) { msgs.erase(m); }
};

void swap(Folder &, Folder &);

#endif // MESSAGE

0 comments on commit c1d0a6e

Please sign in to comment.