Skip to content

Commit

Permalink
added 13.29 ~ 13.32
Browse files Browse the repository at this point in the history
  • Loading branch information
pezy committed Jan 23, 2015
1 parent 7f1b850 commit 70bf0f1
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 106 deletions.
15 changes: 15 additions & 0 deletions ch13/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,18 @@ Copy constructor and copy-assignment operator should dynamicly allocate memory f
## [Exercise 13.27](ex13_27.h)
## Exercise 13.28 [hpp](ex13_28.h) | [cpp](ex13_28.cpp)
## Exercise 13.29:
> Explain why the calls to swap inside swap(HasPtr&, HasPtr&) do not cause a recursion loop.
`swap(lhs.ps, rhs.ps);` feed the version : `swap(std::string*, std::string*)` and `swap(lhs.i, rhs.i);` feed the version : `swap(int, int)`. Both them can't call `swap(HasPtr&, HasPtr&)`. Thus, the calls don't cause a recursion loop.
## [Exercise 13.30](ex13_30.h)
## [Exercise 13.31](ex13_31.h)
## Exercise 13.32:
>Would the pointerlike version of `HasPtr` benefit from defining a swap function? If so, what is the benefit? If not, why not?
@Mooophy:
Essentially, the specific avoiding memory allocation is the reason why it improve performance. As for the pointerlike version, no dynamic memory allocation anyway. Thus, a specific version for it will not improve the performance.
101 changes: 0 additions & 101 deletions ch13/ex13.29.30.31.32.cpp

This file was deleted.

2 changes: 1 addition & 1 deletion ch13/ex13_05.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
class HasPtr {
public:
HasPtr(const std::string &s = std::string()) : ps(new std::string(s)), i(0) { }
HasPtr(const HasPtr& hp) : ps(hp.ps), i(hp.i) { }
HasPtr(const HasPtr& hp) : ps(new std::string(*hp.ps)), i(hp.i) { }
private:
std::string *ps;
int i;
Expand Down
6 changes: 4 additions & 2 deletions ch13/ex13_08.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
class HasPtr {
public:
HasPtr(const std::string &s = std::string()) : ps(new std::string(s)), i(0) { }
HasPtr(const HasPtr &hp) : ps(hp.ps), i(hp.i) { }
HasPtr(const HasPtr &hp) : ps(new std::string(*hp.ps)), i(hp.i) { }
HasPtr& operator=(const HasPtr &hp) {
ps = hp.ps;
std::string *new_ps = new std::string(*hp.ps);
delete ps;
ps = new_ps;
i = hp.i;
return *this;
}
Expand Down
6 changes: 4 additions & 2 deletions ch13/ex13_11.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
class HasPtr {
public:
HasPtr(const std::string &s = std::string()) : ps(new std::string(s)), i(0) { }
HasPtr(const HasPtr &hp) : ps(hp.ps), i(hp.i) { }
HasPtr(const HasPtr &hp) : ps(new std::string(*hp.ps)), i(hp.i) { }
HasPtr& operator=(const HasPtr &hp) {
ps = hp.ps;
std::string *new_ps = new std::string(*hp.ps);
delete ps;
ps = new_ps;
i = hp.i;
return *this;
}
Expand Down
49 changes: 49 additions & 0 deletions ch13/ex13_30.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//
// ex13_30.h
// Exercise 13.30
//
// Created by pezy on 1/23/15.
// Copyright (c) 2015 pezy. All rights reserved.
//
// Write and test a swap function for your valuelike version of HasPtr.
// Give your swap a print statement that notes when it is executed.
//
// See ex13_22.h

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

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
57 changes: 57 additions & 0 deletions ch13/ex13_31.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//
// ex13_31.h
// Exercise 13.31
//
// Created by pezy on 1/23/15.
// Copyright (c) 2015 pezy. All rights reserved.
//
// Give your class a < operator and define a vector of HasPtrs.
// Give that vector some elements and then sort the vector.
// Note when swap is called.
//
// See ex13_30.h

#ifndef CP5_ex13_11_h
#define CP5_ex13_11_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() { 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

0 comments on commit 70bf0f1

Please sign in to comment.