Skip to content

Commit

Permalink
Merge pull request Mooophy#159 from pezy/gcc
Browse files Browse the repository at this point in the history
  • Loading branch information
Mooophy committed Mar 22, 2015
2 parents 7d38d3e + 56f377c commit 4d6b2f3
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
5 changes: 4 additions & 1 deletion ch12/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ b2 is destroyed, but the elements in b2 must not be destroyed.

so b1 and b2 both have 4 elements.

## [Exercise 12.2](ex12_02.h)
## Exercise 12.2

[StrBlob](ex12_02.h) | [TEST](ex12_02_TEST.cpp)

## Exercise 12.3:
>Does this class need const versions of push_back and pop_back? If so, add them. If not, why aren’t they needed?
Expand Down
25 changes: 18 additions & 7 deletions ch12/ex12_02.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,43 @@ using std::vector; using std::string;
class StrBlob {
public:
using size_type = vector<string>::size_type;

StrBlob():data(std::make_shared<vector<string>>()) { }
StrBlob(std::initializer_list<string> il):data(std::make_shared<vector<string>>(il)) { }

size_type size() const { return data->size(); }
bool empty() const { return data->empty(); }

void push_back(const string &t) { data->push_back(t); }
void pop_back() {
check(0, "pop_back on empty StrBlob");
data->pop_back();
}
const std::string& front() {

std::string& front() {
check(0, "front on empty StrBlob");
return data->front();
}

std::string& back() {
check(0, "back on empty StrBlob");
return data->back();
}

const std::string& front() const {
check(0, "front on empty StrBlob");
return data->front();
}
const std::string& back() {
const std::string& back() const {
check(0, "back on empty StrBlob");
return data->back();
}

private:
void check(size_type i, const string &msg) const {
if (i >= data->size()) throw std::out_of_range(msg);
}

private:
std::shared_ptr<vector<string>> data;
};
12 changes: 12 additions & 0 deletions ch12/ex12_02_TEST.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include "ex12_02.h"
#include <iostream>

int main()
{
const StrBlob csb{"hello", "world", "pezy"};
StrBlob sb{"hello", "world", "Mooophy"};

std::cout << csb.front() << " " << csb.back() << std::endl;
sb.back() = "pezy";
std::cout << sb.front() << " " << sb.back() << std::endl;
}

0 comments on commit 4d6b2f3

Please sign in to comment.