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