forked from jaege/Cpp-Primer-5th-Exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
/
12.22.cpp
77 lines (61 loc) · 1.76 KB
/
12.22.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include "StrBlob.h"
#include "ConstStrBlobPtr.h"
void testStrBlob(const StrBlob &sb) {
try {
std::cout << "front: " << sb.front() << " back: " << sb.back() << std::endl;
} catch (std::out_of_range err) {
std::cerr << err.what() << " out of range" << std::endl;
} catch (std::exception err) {
std::cerr << err.what() << std::endl;
}
}
void testConstStrBlobPtr(ConstStrBlobPtr &sbp) {
try {
//sbp.deref() = "Change"; // Error
for (auto p = sbp; ; p.inc())
std::cout << "deref: " << p.deref() << std::endl;
} catch (std::out_of_range err) {
std::cerr << err.what() << " out of range" << std::endl;
} catch (std::exception err) {
std::cerr << err.what() << std::endl;
}
}
int main() {
StrBlob sb1;
StrBlob sb2{"Hello", "World"};
const StrBlob csb1;
testStrBlob(csb1);
std::cout << std::endl;
const StrBlob csb2{"This", "Blob"};
testStrBlob(csb2);
std::cout << std::endl;
ConstStrBlobPtr csbp1;
testConstStrBlobPtr(csbp1);
std::cout << std::endl;
ConstStrBlobPtr sbp2(sb1);
testConstStrBlobPtr(sbp2);
std::cout << std::endl;
ConstStrBlobPtr sbp3(sb1, sb1.size());
testConstStrBlobPtr(sbp3);
std::cout << std::endl;
ConstStrBlobPtr sbp4(sb2);
testConstStrBlobPtr(sbp4);
std::cout << std::endl;
ConstStrBlobPtr sbp5(sb2, sb2.size());
testConstStrBlobPtr(sbp5);
std::cout << std::endl;
ConstStrBlobPtr csbp2(csb1);
testConstStrBlobPtr(csbp2);
std::cout << std::endl;
ConstStrBlobPtr csbp3(csb1, csb1.size());
testConstStrBlobPtr(csbp3);
std::cout << std::endl;
ConstStrBlobPtr csbp4(csb2);
testConstStrBlobPtr(csbp4);
std::cout << std::endl;
ConstStrBlobPtr csbp5(csb2, csb2.size());
testConstStrBlobPtr(csbp5);
std::cout << std::endl;
return 0;
}