forked from huangmingchuan/Cpp_Primer_Answers
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cd2b717
commit cf28223
Showing
5 changed files
with
292 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#ifndef CP5_ex13_11_h | ||
#define CP5_ex13_11_h | ||
|
||
#include <string> | ||
#include <iostream> | ||
|
||
class HasPtr | ||
{ | ||
public: | ||
friend void swap(HasPtr&, HasPtr&); | ||
HasPtr(const std::string &s = std::string()) : ps(new std::string(s)), i(0) {} | ||
HasPtr(const HasPtr &hp) : ps(new std::string(*hp.ps)), i(hp.i) {} | ||
HasPtr& operator=(const HasPtr &hp) | ||
{ | ||
auto new_p = new std::string(*hp.ps); | ||
delete ps; | ||
ps = new_p; | ||
i = hp.i; | ||
return *this; | ||
} | ||
~HasPtr() | ||
{ | ||
delete ps; | ||
} | ||
|
||
void show() { std::cout << *ps << std::endl; } | ||
private: | ||
std::string *ps; | ||
int i; | ||
}; | ||
|
||
inline | ||
void swap(HasPtr& lhs, HasPtr& rhs) | ||
{ | ||
using std::swap; | ||
swap(lhs.ps, rhs.ps); | ||
swap(lhs.i, rhs.i); | ||
std::cout << "call swap(HasPtr& lhs, HasPtr& rhs)" << std::endl; | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#ifndef CP5_ex13_31_h | ||
#define CP5_ex13_31_h | ||
|
||
#include <string> | ||
#include <iostream> | ||
|
||
class HasPtr | ||
{ | ||
public: | ||
friend void swap(HasPtr&, HasPtr&); | ||
friend bool operator<(const HasPtr &lhs, const HasPtr &rhs); | ||
|
||
HasPtr(const std::string &s = std::string()) | ||
: ps(new std::string(s)), i(0) | ||
{} | ||
|
||
HasPtr(const HasPtr &hp) | ||
: ps(new std::string(*hp.ps)), i(hp.i) | ||
{} | ||
|
||
HasPtr& operator=(HasPtr tmp) | ||
{ | ||
this->swap(tmp); | ||
return *this; | ||
} | ||
|
||
~HasPtr() | ||
{ | ||
delete ps; | ||
} | ||
|
||
void swap(HasPtr &rhs) | ||
{ | ||
using std::swap; | ||
swap(ps, rhs.ps); | ||
swap(i, rhs.i); | ||
std::cout << "call swap(HasPtr &rhs)" << std::endl; | ||
} | ||
|
||
void show() const | ||
{ | ||
std::cout << *ps << std::endl; | ||
} | ||
private: | ||
std::string *ps; | ||
int i; | ||
}; | ||
|
||
void swap(HasPtr& lhs, HasPtr& rhs) | ||
{ | ||
lhs.swap(rhs); | ||
} | ||
|
||
bool operator<(const HasPtr &lhs, const HasPtr &rhs) | ||
{ | ||
return *lhs.ps < *rhs.ps; | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
#include "exercise13_34.h" | ||
#include <iostream> | ||
|
||
void 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); | ||
} | ||
|
||
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); | ||
} | ||
|
||
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); | ||
folders.clear(); | ||
} | ||
|
||
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() | ||
{ | ||
std::cout << contents << std::endl; | ||
} | ||
|
||
void swap(Folder& lhs, Folder& rhs) | ||
{ | ||
using std::swap; | ||
for (auto m : lhs.msgs) | ||
m->remFlddr(&lhs); | ||
for (auto m : rhs.msgs) | ||
m->remFlddr(&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) | ||
m->addFldr(this); | ||
} | ||
|
||
Folder::Folder(const Folder& f) :msgs(f.msgs) | ||
{ | ||
add_to_Message(f); | ||
} | ||
|
||
void Folder::remove_to_Message() | ||
{ | ||
for (auto m : msgs) | ||
m->remFlddr(this); | ||
msgs.clear(); | ||
} | ||
|
||
Folder::~Folder() | ||
{ | ||
remove_to_Message(); | ||
} | ||
|
||
Folder& Folder::operator=(const Folder& rhs) | ||
{ | ||
remove_to_Message(); | ||
msgs = rhs.msgs; | ||
add_to_Message(rhs); | ||
return *this; | ||
} | ||
|
||
void Folder::print_debug() | ||
{ | ||
for (auto m : msgs) | ||
std::cout << m->contents << " "; | ||
std::cout << std::endl; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#ifndef ex13_34_h | ||
#define ex13_34_h | ||
|
||
#include <string> | ||
#include <set> | ||
|
||
class Folder; | ||
|
||
class Message | ||
{ | ||
friend void swap(Message &, Message &); | ||
friend void swap(Folder &, Folder &); | ||
friend class Folder; | ||
public: | ||
explicit Message(const std::string& s = "") :contents(s) {} | ||
Message(const Message&); | ||
Message& operator=(const Message&); | ||
~Message(); | ||
void save(Folder&); | ||
void remove(Folder&); | ||
void print_debug(); | ||
|
||
private: | ||
std::string contents; | ||
std::set<Folder*> folders; | ||
|
||
void add_to_Folders(const Message&); | ||
void remove_from_Folders(); | ||
|
||
void addFldr(Folder* f) { folders.insert(f); } | ||
void remFlddr(Folder* f) { folders.erase(f); } | ||
}; | ||
|
||
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; | ||
|
||
void add_to_Message(const Folder&); | ||
void remove_to_Message(); | ||
|
||
void addMsg(Message* m) { msgs.insert(m); } | ||
void remMsg(Message *m) { msgs.erase(m); } | ||
}; | ||
|
||
void swap(Folder&, Folder&); | ||
|
||
|
||
#endif // !ex13_34_h |