Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Apr 21, 2024. It is now read-only.

Commit 414708e

Browse files
committed
Finish Chapter 12
1 parent 2b1acf3 commit 414708e

32 files changed

+1268
-0
lines changed

‎README.md‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,21 @@ Lambda expression provides a wrapping for functions to make it takes the require
471471
- `bind`
472472
473473
474+
## Forward Declaration
475+
The *forward declration* `class StrBlob;` introduces the name `StrBlob` into the program and indicates that it's a class type. **After the declaration and before a definition is seen, the class is a incomplete type:** it is know that `StrBlob` is a class type but what members the type contains is unknown.
476+
477+
We can use a incomplete type only in limited ways:
478+
1. we can define pointers or references to such type.
479+
2. We can declare, but not define, functions using the incomplete type as return or parameter type.
480+
481+
A class must be defined before:
482+
1. we can create object of that type, otherwise the compiler does not know how much memory to allocate for the object.
483+
2. we can access the members of the class.
484+
3. Being used as the member of another class.
485+
486+
487+
488+
474489
475490
476491

‎ch12/ex12_1.h‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include "headers.h"
2+
3+
class StrBlob {
4+
public:
5+
using size_type = vector<string>::size_type;
6+
7+
StrBlob();
8+
StrBlob(initializer_list<string> il);
9+
10+
size_type size() { return data->size(); }
11+
bool empty() { return data->empty(); }
12+
void push_back(const string & s) { data -> push_back(s); }
13+
string &front();
14+
const string &front() const;
15+
string &back();
16+
const string &back() const;
17+
18+
private:
19+
shared_ptr<vector<string>> data;
20+
void check(size_type i, const string &msg) const;
21+
};
22+
23+
StrBlob::StrBlob() : data(make_shared<vector<string>>()) {}
24+
StrBlob::StrBlob(initializer_list<string> il) : data(make_shared<vector<string>>(il)) {}
25+
26+
void StrBlob::check(StrBlob::size_type i, const string &msg) const {
27+
if ( i >= data->size()){
28+
throw out_of_range(msg);
29+
}
30+
}
31+
32+
string & StrBlob::front(){
33+
check(0, "front at empty StrBlob");
34+
return data -> front();
35+
}
36+
37+
const string & StrBlob::front() const{
38+
check(0, "front at empty StrBlob");
39+
return data -> front();
40+
}
41+
42+
string & StrBlob::back(){
43+
check(0, "back at empty StrBlob");
44+
return data -> back();
45+
}
46+
47+
const string & StrBlob::back() const{
48+
check(0, "back at empty StrBlob");
49+
return data -> back();
50+
}

‎ch12/ex12_14.cpp‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include "headers.h"
2+
3+
class Connection {
4+
public:
5+
Connection(string dst) {
6+
cout << "Connection to " << dst << " created!" << endl;
7+
}
8+
};
9+
10+
Connection connect(string dst) {
11+
Connection &c = *(new Connection(dst));
12+
cout << "Connection to " << dst << " conncted!" << endl;
13+
return c;
14+
}
15+
16+
void end_connection(Connection * pc){
17+
delete pc;
18+
pc = nullptr;
19+
cout << "Connection ended!" << endl;
20+
}
21+
22+
void fn() {
23+
Connection c = connect("USA");
24+
shared_ptr<Connection> spc(&c, end_connection);
25+
}
26+
27+
int main() {
28+
fn();
29+
}

‎ch12/ex12_15.cpp‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include "headers.h"
2+
3+
class Connection {
4+
public:
5+
Connection(string dst) {
6+
cout << "Connection to " << dst << " created!" << endl;
7+
}
8+
};
9+
10+
Connection connect(string dst) {
11+
Connection &c = *(new Connection(dst));
12+
cout << "Connection to " << dst << " conncted!" << endl;
13+
return c;
14+
}
15+
16+
void fn() {
17+
Connection c = connect("USA");
18+
shared_ptr<Connection> spc(&c,
19+
[](Connection *pc) -> void {delete pc; pc = nullptr; cout << "Connection ended!" << endl; });
20+
}
21+
22+
int main() {
23+
fn();
24+
}

‎ch12/ex12_16.cpp‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include "headers.h"
2+
3+
int main() {
4+
unique_ptr<string> ups1(new string("Kobe"));
5+
// unique_ptr<string> ups2(ups1);
6+
// unique_ptr<string> ups3 = ups1;
7+
}

‎ch12/ex12_19.cpp‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include "headers.h"
2+
// #include "ex12_19_StrBlob.h"
3+
// #include "ex12_19_StrBlobPtr.h"
4+
#include "ex12_19_combined.h"
5+
6+
int main() {
7+
StrBlob sb{"hu", "jdkf"};
8+
StrBlobPtr sbp(sb, 0);
9+
sb.push_back("fhdj");
10+
11+
cout << sbp.deref() << endl;
12+
cout << sbp.incre().deref() << endl;
13+
}

‎ch12/ex12_19_StrBlob.h‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include "headers.h"
2+
3+
class StrBlobPtr;
4+
5+
class StrBlob {
6+
friend class StrBlobPtr;
7+
8+
public:
9+
using size_type = vector<string>::size_type;
10+
11+
StrBlob();
12+
StrBlob(initializer_list<string> il);
13+
14+
size_type size() { return data->size(); }
15+
bool empty() { return data->empty(); }
16+
void push_back(const string &s) { data->push_back(s); }
17+
string &front();
18+
const string &front() const;
19+
string &back();
20+
const string &back() const;
21+
StrBlobPtr begin();
22+
StrBlobPtr end();
23+
24+
private:
25+
shared_ptr<vector<string>> data;
26+
void check(size_type i, const string &msg) const;
27+
};
28+
29+
StrBlob::StrBlob() : data(make_shared<vector<string>>()) {}
30+
StrBlob::StrBlob(initializer_list<string> il) : data(make_shared<vector<string>>(il)) {}
31+
32+
void StrBlob::check(StrBlob::size_type i, const string &msg) const {
33+
if (i >= data->size()) {
34+
throw out_of_range(msg);
35+
}
36+
}
37+
38+
string &StrBlob::front() {
39+
check(0, "front at empty StrBlob");
40+
return data->front();
41+
}
42+
43+
const string &StrBlob::front() const {
44+
check(0, "front at empty StrBlob");
45+
return data->front();
46+
}
47+
48+
string &StrBlob::back() {
49+
check(0, "back at empty StrBlob");
50+
return data->back();
51+
}
52+
53+
const string &StrBlob::back() const {
54+
check(0, "back at empty StrBlob");
55+
return data->back();
56+
}

‎ch12/ex12_19_StrBlobPtr.h‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include "headers.h"
2+
3+
class StrBlob;
4+
5+
// StrBlobPtr is a class provided for
6+
// user of StrBlob class to access
7+
// string elements stored in the StrBlob object
8+
class StrBlobPtr {
9+
public:
10+
StrBlobPtr() : curr(0) {}
11+
StrBlobPtr(StrBlob &sb, size_t init_idx = 0) : wptr(sb.data), curr(init_idx) {}
12+
string &deref();
13+
StrBlobPtr &incre();
14+
15+
private:
16+
shared_ptr<vector<string>> check(size_t i, const string &msg) const;
17+
weak_ptr<vector<string>> wptr;
18+
size_t curr;
19+
};
20+
21+
shared_ptr<vector<string>> StrBlobPtr::check(size_t i, const string &msg) const {
22+
auto sp = wptr.lock();
23+
if (!sp) {
24+
throw runtime_error("Memory already freed!");
25+
}
26+
if (i >= sp->size()) {
27+
throw out_of_range(msg);
28+
}
29+
30+
return sp;
31+
}
32+
33+
string &StrBlobPtr::deref() {
34+
auto p = check(curr, "dereference past the end");
35+
return (*p)[curr];
36+
}
37+
38+
StrBlobPtr &StrBlobPtr::incre() {
39+
check(curr, "increment past end of StrBlob");
40+
++curr;
41+
return *this;
42+
}

‎ch12/ex12_19_combined.h‎

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
#ifndef STRBLOB_STRBLOBPTR_COMBINED
2+
#define STRBLOB_STRBLOBPTR_COMBINED
3+
4+
#include "headers.h"
5+
6+
class StrBlobPtr;
7+
8+
class StrBlob {
9+
friend class StrBlobPtr;
10+
11+
public:
12+
using size_type = vector<string>::size_type;
13+
14+
StrBlob();
15+
StrBlob(initializer_list<string> il);
16+
17+
size_type size() { return data->size(); }
18+
bool empty() { return data->empty(); }
19+
void push_back(const string &s) { data->push_back(s); }
20+
string &front();
21+
const string &front() const;
22+
string &back();
23+
const string &back() const;
24+
StrBlobPtr begin();
25+
StrBlobPtr end();
26+
27+
private:
28+
shared_ptr<vector<string>> data;
29+
void check(size_type i, const string &msg) const;
30+
};
31+
32+
StrBlob::StrBlob() : data(make_shared<vector<string>>()) {}
33+
StrBlob::StrBlob(initializer_list<string> il) : data(make_shared<vector<string>>(il)) {}
34+
35+
void StrBlob::check(StrBlob::size_type i, const string &msg) const {
36+
if (i >= data->size()) {
37+
throw out_of_range(msg);
38+
}
39+
}
40+
41+
string &StrBlob::front() {
42+
check(0, "front at empty StrBlob");
43+
return data->front();
44+
}
45+
46+
const string &StrBlob::front() const {
47+
check(0, "front at empty StrBlob");
48+
return data->front();
49+
}
50+
51+
string &StrBlob::back() {
52+
check(0, "back at empty StrBlob");
53+
return data->back();
54+
}
55+
56+
const string &StrBlob::back() const {
57+
check(0, "back at empty StrBlob");
58+
return data->back();
59+
}
60+
61+
class StrBlobPtr {
62+
public:
63+
StrBlobPtr() : curr(0) {}
64+
StrBlobPtr(StrBlob &sb, size_t init_idx = 0) : wptr(sb.data), curr(init_idx) {}
65+
string &deref();
66+
StrBlobPtr &incre();
67+
68+
private:
69+
shared_ptr<vector<string>> check(size_t i, const string &msg) const;
70+
weak_ptr<vector<string>> wptr;
71+
size_t curr;
72+
};
73+
74+
shared_ptr<vector<string>> StrBlobPtr::check(size_t i, const string &msg) const {
75+
auto sp = wptr.lock();
76+
if (!sp) {
77+
throw runtime_error("Memory already freed!");
78+
}
79+
if (i >= sp->size()) {
80+
throw out_of_range(msg);
81+
}
82+
83+
return sp;
84+
}
85+
86+
string &StrBlobPtr::deref() {
87+
auto p = check(curr, "dereference past the end");
88+
return (*p)[curr];
89+
}
90+
91+
StrBlobPtr &StrBlobPtr::incre() {
92+
check(curr, "increment past end of StrBlob");
93+
++curr;
94+
return *this;
95+
}
96+
97+
98+
StrBlobPtr StrBlob::begin() {
99+
return StrBlobPtr(*this);
100+
}
101+
102+
StrBlobPtr StrBlob::end() {
103+
return StrBlobPtr(*this, data->size());
104+
}
105+
106+
#endif

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /