diff options
Diffstat (limited to 'ue5/verwendung.cpp')
| -rw-r--r-- | ue5/verwendung.cpp | 218 |
1 files changed, 218 insertions, 0 deletions
diff --git a/ue5/verwendung.cpp b/ue5/verwendung.cpp new file mode 100644 index 0000000..b3506c0 --- /dev/null +++ b/ue5/verwendung.cpp | |||
| @@ -0,0 +1,218 @@ | |||
| 1 | #include <cstddef> // for std::size_t | ||
| 2 | #include <cassert> // for assert | ||
| 3 | #include <utility> // for std::move | ||
| 4 | #include <stdexcept> // for std::out_of_range | ||
| 5 | |||
| 6 | struct Person | ||
| 7 | { | ||
| 8 | Person (){} | ||
| 9 | virtual int mark(){ return 0; } | ||
| 10 | virtual ~Person(){} | ||
| 11 | }; | ||
| 12 | |||
| 13 | struct Student : Person | ||
| 14 | { | ||
| 15 | int m_mark; | ||
| 16 | Student (int mark) : | ||
| 17 | m_mark(mark) | ||
| 18 | {} | ||
| 19 | int mark() | ||
| 20 | { | ||
| 21 | return m_mark; | ||
| 22 | } | ||
| 23 | }; | ||
| 24 | |||
| 25 | struct Pupil : Person | ||
| 26 | { | ||
| 27 | int m_mark; | ||
| 28 | Pupil (int mark) : | ||
| 29 | m_mark(mark) | ||
| 30 | {} | ||
| 31 | int mark() | ||
| 32 | { | ||
| 33 | return m_mark; | ||
| 34 | } | ||
| 35 | }; | ||
| 36 | |||
| 37 | #include <memory> | ||
| 38 | #include "shared_ptr.hpp" | ||
| 39 | |||
| 40 | #ifdef SOLVED_1 | ||
| 41 | using Ti::shared_ptr; | ||
| 42 | using Ti::make_shared; | ||
| 43 | using Ti::shared_dynamic_cast; | ||
| 44 | #else | ||
| 45 | using std::shared_ptr; | ||
| 46 | using std::make_shared; | ||
| 47 | #endif | ||
| 48 | |||
| 49 | #include <array> | ||
| 50 | #include "array.hpp" | ||
| 51 | |||
| 52 | #ifdef SOLVED_2 | ||
| 53 | using Ti::array; | ||
| 54 | using Ti::make_array; | ||
| 55 | #else | ||
| 56 | using std::array; | ||
| 57 | #endif | ||
| 58 | |||
| 59 | #include "mean_mark.hpp" | ||
| 60 | #ifdef SOLVED_3 | ||
| 61 | using Ti::mean_mark; | ||
| 62 | using Ti::mean_mark_student; | ||
| 63 | using Ti::remove_greater; | ||
| 64 | #endif | ||
| 65 | |||
| 66 | #include <vector> | ||
| 67 | using std::vector; | ||
| 68 | |||
| 69 | int main() | ||
| 70 | { | ||
| 71 | try { | ||
| 72 | // initialize with new expression | ||
| 73 | shared_ptr<Student> n1 (new Student(5)); | ||
| 74 | assert (n1.get() != 0); | ||
| 75 | shared_ptr<Person> n2 (new Person); | ||
| 76 | assert (n2.get() != 0); | ||
| 77 | |||
| 78 | throw 0; // must be exception safe | ||
| 79 | } catch (...) | ||
| 80 | {} | ||
| 81 | |||
| 82 | |||
| 83 | { | ||
| 84 | // derived1 is 0. | ||
| 85 | shared_ptr<Student> derived1; | ||
| 86 | assert(derived1.get() == 0); | ||
| 87 | |||
| 88 | // Other way for object creation | ||
| 89 | derived1 = make_shared<Student>(3); | ||
| 90 | assert(derived1.get() != 0); | ||
| 91 | |||
| 92 | // Call object member | ||
| 93 | derived1->mark(); | ||
| 94 | |||
| 95 | // Object creation with constructor parameters | ||
| 96 | shared_ptr<Student> derived2 = make_shared<Student>(4); | ||
| 97 | assert (derived2.get() != 0); | ||
| 98 | |||
| 99 | shared_ptr<Person> base1 = make_shared<Person>(); | ||
| 100 | assert (base1.get() != 0); | ||
| 101 | |||
| 102 | // Implicit upcast possible. The object make_sharedd in the previous line is | ||
| 103 | // destroyed, because there are no more pointers referencing it. | ||
| 104 | base1 = derived1; | ||
| 105 | |||
| 106 | #ifdef SOLVED_1 | ||
| 107 | // Explicit downcast possible. Some casts that are available: constCast, | ||
| 108 | // staticCast, dynamicCast | ||
| 109 | derived2 = shared_dynamic_cast<Student>(base1); | ||
| 110 | |||
| 111 | // You can compare pointers. | ||
| 112 | assert(derived1 == derived2); | ||
| 113 | #endif | ||
| 114 | |||
| 115 | // Destroy most references to derived instance. References | ||
| 116 | // (but not the object itself) are destroyed if they go out | ||
| 117 | // of scope, or you can force reference destruction by multiple ways. | ||
| 118 | derived1.reset(); // release reference | ||
| 119 | assert(derived1.get() == 0); | ||
| 120 | } | ||
| 121 | |||
| 122 | |||
| 123 | { | ||
| 124 | // array<int, 0> x; // Should fail with static assertion | ||
| 125 | |||
| 126 | typedef array<shared_ptr<Student>, 5> sarray; | ||
| 127 | sarray a; | ||
| 128 | a [0] = shared_ptr<Student>(new Student(2)); | ||
| 129 | a [1] = shared_ptr<Student>(new Student(1)); | ||
| 130 | a [2] = shared_ptr<Student>(new Student(5)); | ||
| 131 | a [3] = shared_ptr<Student>(new Student(4)); | ||
| 132 | a [4] = shared_ptr<Student>(); | ||
| 133 | assert(a[0].get() != 0); | ||
| 134 | assert(a[4].get() == 0); | ||
| 135 | a [4] = shared_ptr<Student>(new Student(4)); | ||
| 136 | |||
| 137 | try { | ||
| 138 | a.at(5); // throws exception | ||
| 139 | assert(0); | ||
| 140 | } catch (std::out_of_range const& oor) { | ||
| 141 | oor.what(); | ||
| 142 | } catch (...) { | ||
| 143 | assert(0); | ||
| 144 | } | ||
| 145 | |||
| 146 | #ifdef SOLVED_2 | ||
| 147 | array<int,3> a1 = make_array<int,3>(); | ||
| 148 | a1.fill(3); | ||
| 149 | |||
| 150 | array<int,3> a2 = make_array<int,3>(); | ||
| 151 | a2.fill(4); | ||
| 152 | |||
| 153 | a1.swap(a2); | ||
| 154 | assert(a1[0] == 4); | ||
| 155 | assert(a2[2] == 3); | ||
| 156 | #endif | ||
| 157 | |||
| 158 | #ifdef SOLVED_3 | ||
| 159 | double mean = mean_mark(a.begin(), a.end()); | ||
| 160 | assert (mean >= 3.1 || mean <= 3.3); | ||
| 161 | |||
| 162 | double mean_student = mean_mark_student(a.begin(), a.end()); | ||
| 163 | assert (mean_student >= 3.1 || mean_student <= 3.3); | ||
| 164 | |||
| 165 | sarray::iterator end = remove_greater(a.begin(), a.end(), 3); | ||
| 166 | double mean2 = mean_mark(a.begin(), end); | ||
| 167 | assert (mean2 >= 1.4 || mean2 <= 1.6); | ||
| 168 | #endif | ||
| 169 | } | ||
| 170 | |||
| 171 | |||
| 172 | { | ||
| 173 | typedef array<shared_ptr<Person>, 5> parray; | ||
| 174 | parray m; | ||
| 175 | m [0] = shared_ptr<Student>(new Student(2)); | ||
| 176 | m [1] = shared_ptr<Pupil>(new Pupil(1)); | ||
| 177 | m [2] = shared_ptr<Student>(new Student(5)); | ||
| 178 | m [3] = shared_ptr<Student>(new Student(4)); | ||
| 179 | m [4] = shared_ptr<Person>(new Person()); | ||
| 180 | |||
| 181 | #ifdef SOLVED_3 | ||
| 182 | double mean = mean_mark(m.begin(), m.end()); | ||
| 183 | assert (mean >= 2.3 || mean <= 2.5); | ||
| 184 | |||
| 185 | double mean_student = mean_mark_student(m.begin(), m.end()); | ||
| 186 | assert (mean_student >= 3.6 || mean_student <= 3.8); | ||
| 187 | |||
| 188 | parray::iterator end = remove_greater(m.begin(), m.end(), 3); | ||
| 189 | double mean2 = mean_mark(m.begin(), end); | ||
| 190 | assert (mean2 >= 0.9 || mean2 <= 1.1); | ||
| 191 | #endif | ||
| 192 | } | ||
| 193 | |||
| 194 | { | ||
| 195 | vector<shared_ptr<Person>> m; | ||
| 196 | m.push_back(shared_ptr<Student>(new Student(2))); | ||
| 197 | m.push_back(shared_ptr<Pupil>(new Pupil(1))); | ||
| 198 | m.push_back(shared_ptr<Student>(new Student(5))); | ||
| 199 | m.push_back(shared_ptr<Student>(new Student(4))); | ||
| 200 | m.push_back(shared_ptr<Person>(new Person())); | ||
| 201 | |||
| 202 | #ifdef SOLVED_3 | ||
| 203 | double mean = mean_mark(m.begin(), m.end()); | ||
| 204 | assert (mean >= 2.3 || mean <= 2.5); | ||
| 205 | |||
| 206 | double mean_student = mean_mark_student(m.begin(), m.end()); | ||
| 207 | assert (mean_student >= 3.6 || mean_student <= 3.8); | ||
| 208 | |||
| 209 | assert(m.size() == 5); | ||
| 210 | m.erase(remove_greater(m.begin(), m.end(), 3), m.end()); | ||
| 211 | assert(m.size() == 3); | ||
| 212 | |||
| 213 | double mean2 = mean_mark(m.begin(), m.end()); | ||
| 214 | assert (mean2 >= 0.9 || mean2 <= 1.1); | ||
| 215 | #endif | ||
| 216 | } | ||
| 217 | |||
| 218 | } | ||
