-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathExercises_15_SmartPointers.cpp
More file actions
1016 lines (770 loc) · 28.5 KB
/
Copy pathExercises_15_SmartPointers.cpp
File metadata and controls
1016 lines (770 loc) · 28.5 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// =====================================================================================
// Exercises_15_SmartPointers.cpp
// =====================================================================================
module;
#define _CRTDBG_MAP_ALLOC
#include <cstdlib>
#include <crtdbg.h>
#ifdef _DEBUG
#ifndef DBG_NEW
#define DBG_NEW new ( _NORMAL_BLOCK , __FILE__ , __LINE__ )
#define new DBG_NEW
#endif
#endif // _DEBUG
#include <cassert>
#include <span>
module modern_cpp_exercises:smart_pointers;
import std;
namespace Exercises_SmartPointers {
namespace Exercise_01 {
// =============================================================
// Counting references :)
static void testExercise() {
class X {};
std::shared_ptr<X> pA;
std::shared_ptr<X> pB;
std::shared_ptr<X> pC;
pA = std::make_shared<X>(); // use-count always starts at 1
assert(pA.use_count() == 1);
pB = pA; // make a copy of the pointer; use-count is now 2
assert(pA.use_count() == 2);
assert(pB.use_count() == 2);
pC = std::move(pA); // moving the pointer keeps the use-count at 2
assert(pA == nullptr);
assert(pB.use_count() == 2);
assert(pC.use_count() == 2);
pB = nullptr; // decrement the use-count back to 1
assert(pA == nullptr);
assert(pB == nullptr);
assert(pC.use_count() == 1);
}
}
namespace Exercise_02 {
// =============================================================
// Don't double-manage!
static void testExercise() {
class X
{
public:
X() : m_value{ 123 } {}
int getValue() const { return m_value; }
private:
int m_value;
};
std::shared_ptr<X> pA{ new X{} }; // use-count always starts at 1
std::shared_ptr<X> pB;
std::shared_ptr<X> pC;
assert(pA.use_count() == 1);
pB = pA; // make a copy of the pointer; use-count is now 2
assert(pA.use_count() == 2);
assert(pB.use_count() == 2);
pC = std::shared_ptr<X>{ pB.get() };
// ERROR! Don't double-manage a raw pointer!
// Give never the same pointer to a shared_ptr object again,
// which would tell this shared_ptr to manage it -- twice!
assert(pA.use_count() == 2);
assert(pB.use_count() == 2);
assert(pC.use_count() == 1);
pC = nullptr; // or pC.reset();
// pC's use-count drops to zero,
// shared_ptr calls "delete" on the X object
assert(pA.use_count() == 2);
assert(pB.use_count() == 2);
assert(pC == nullptr);
int value = (*pB).getValue(); // accessing the freed object yields undefined behavior
std::cout << "Value: " << value << std::endl;
}
}
namespace Exercise_03 {
// =============================================================
// Considering a "non-owning reference"
class UnsafeWatcher
{
private:
int* m_ptr;
public:
UnsafeWatcher() : m_ptr{ nullptr } {}
void watch(const std::shared_ptr<int>& sp)
{
m_ptr = sp.get();
}
int currentValue() const
{
return *m_ptr; // m_ptr might have been released !
}
};
static void testExercise_01()
{
UnsafeWatcher watcher;
{
std::shared_ptr<int> sp{ std::make_shared<int>(123) };
watcher.watch(sp);
std::cout << "Value: " << watcher.currentValue() << std::endl;
}
std::cout << "Value: " << watcher.currentValue() << std::endl;
}
class HeavyAndSafeWatcher {
private:
std::shared_ptr<int> m_ptr;
public:
HeavyAndSafeWatcher() {}
void watch(const std::shared_ptr<int>& sp)
{
m_ptr = sp;
}
int currentValue() const
{
return *m_ptr; // m_ptr is always alive!
}
};
static void testExercise_02()
{
HeavyAndSafeWatcher watcher;
{
std::shared_ptr<int> sp{ std::make_shared<int>(123) };
watcher.watch(sp);
std::cout << "Value: " << watcher.currentValue() << std::endl;
}
std::cout << "Value: " << watcher.currentValue() << std::endl;
}
class LightweightAndSafeWatcher {
private:
std::weak_ptr<int> m_ptr;
public:
LightweightAndSafeWatcher() = default;
void watch(const std::shared_ptr<int>& sp)
{
m_ptr = sp;
}
int currentValue() const
{
// Now we can safely ask whether *m_ptr has been deallocated or not
std::shared_ptr<int> sp{ m_ptr.lock() };
if (sp != nullptr)
{
return *sp;
}
else
{
throw std::exception{ "No value available!" };
}
}
};
static void testExercise_03() {
LightweightAndSafeWatcher watcher;
{
std::shared_ptr<int> sp{ std::make_shared<int>(123) };
watcher.watch(sp);
std::cout << "Value: " << watcher.currentValue() << std::endl;
}
try {
std::cout << "Value: " << watcher.currentValue() << std::endl;
}
catch (const std::exception& ex) {
std::cout << ex.what() << std::endl;
}
}
static void testExercise() {
testExercise_01();
testExercise_02();
testExercise_03();
}
}
namespace Exercise_04 {
// =============================================================
// MyString implementation using std::shared_ptr
// Note:
// This is a theoretical implementation of the task.
// In practice, there is exactly one owner of the string within the class.
// Therefore, the class `std: :unique_ptr` would be the appropriate implementation method.
// Note the side effects of both implementations:
//
// The Rule of Zero could be applied to the `MyStringSP` class.
// However, the resulting behavior of the `MyStringSP` class is incorrect,
// because copies of `MyStringSP` objects are not real copies (shallow-copy behavior)!
//
// The Rule of Zero is not applicable to the `MyStringUP` class,
// because attempting to copy or assign `MyStringUP` objects results in error messages.
// The copy constructor and the assignment operator must be explicitly implemented
// (with deep-copy behavior).
//
// In both implementations, a destructor can be omitted.
// The destructor of a `std::shared_ptr` or a `std::unique_ptr` object
// is automatically called as being a child object of a parent object in both cases.
class MyStringSP
{
private:
std::shared_ptr<char[]> m_string;
std::size_t m_length;
public:
// default c'tor
MyStringSP() : m_length{} {}
// user defined c'tor
MyStringSP(const char* string)
{
m_length = std::strlen(string);
m_string = std::make_shared<char[]>(m_length + 1);
std::memcpy(m_string.get(), string, m_length + 1); // incl. copying '\0'
}
// no destructor necessary
// public interface
void print()
{
if (m_string.get() == nullptr) {
std::println("<null>");
}
else {
std::println("{}", m_string.get());
}
}
// getter
std::size_t length() const { return m_length; }
// subscript operator
char& operator[] (int index) {
if (index >= m_length) {
throw std::out_of_range("Wrong index!");
}
return m_string.get()[index];
}
// copy c'tor
MyStringSP(const MyStringSP& other) {
// shallow (flat) copy
// m_string = other.m_string;
// m_length = other.m_length;
// or deep copy
m_length = other.m_length;
m_string = std::make_shared<char[]>(m_length + 1);
std::memcpy(m_string.get(), other.m_string.get(), m_length + 1); // copying '\0'
}
MyStringSP& operator=(const MyStringSP& other) {
if (this == &other) {
return *this;
}
// shallow (flat) copy
// m_string = other.m_string;
// m_length = other.m_length;
// or deep copy
m_length = other.m_length;
m_string = std::make_shared<char[]>(m_length + 1);
std::memcpy(m_string.get(), other.m_string.get(), m_length + 1); // copying '\0'
return *this;
}
// move c'tor
MyStringSP(MyStringSP&& other) noexcept {
m_string = std::move(other.m_string);
m_length = other.m_length;
other.m_length = 0;
}
// move assignment
MyStringSP& operator=(MyStringSP&& other) noexcept {
if (this == &other) {
return *this;
}
m_string = std::move(other.m_string);
m_length = other.m_length;
other.m_length = 0;
return *this;
}
};
// =============================================================
// MyString implementation using std::unique_ptr
class MyStringUP
{
private:
std::unique_ptr<char[]> m_string;
std::size_t m_length;
public:
// default c'tor
MyStringUP() : m_length{} {}
// user defined c'tor
MyStringUP(const char* string)
{
m_length = std::strlen(string);
m_string = std::make_unique<char[]>(m_length + 1);
std::memcpy(m_string.get(), string, m_length + 1); // copying '\0'
}
// no destructor necessary
// public interface
void print()
{
if (m_string.get() == nullptr) {
std::println("<null>");
}
else {
std::println("{}", m_string.get());
}
}
// subscript operator
char& operator[] (int index) {
if (index >= m_length) {
throw std::out_of_range("Wrong index!");
}
return m_string.get()[index];
}
// copy c'tor
MyStringUP(const MyStringUP& other) {
m_length = std::strlen(other.m_string.get());
m_string = std::make_unique<char[]>(m_length + 1);
std::memcpy(m_string.get(), other.m_string.get(), m_length + 1); // copying '\0'
}
// copy assignment
MyStringUP& operator=(const MyStringUP& other) {
if (this == &other) {
return *this;
}
m_length = std::strlen(other.m_string.get());
m_string = std::make_unique<char[]>(m_length + 1);
std::memcpy(m_string.get(), other.m_string.get(), m_length + 1); // copying '\0'
return *this;
}
// move c'tor
MyStringUP(MyStringUP&& other) noexcept {
m_string = std::move(other.m_string);
m_length = other.m_length;
other.m_length = 0;
}
// move assignment
MyStringUP& operator=(MyStringUP&& other) noexcept {
if (this == &other) {
return *this;
}
m_string = std::move(other.m_string);
m_length = other.m_length;
other.m_length = 0;
return *this;
}
};
// =============================================================
// test routines
static void testExercise_00()
{
MyStringSP ms1{ };
ms1.print();
MyStringUP ms2{ };
ms2.print();
}
static void testExercise_01()
{
MyStringSP ms1{ "123" };
ms1.print();
MyStringSP ms2{ "ABCDE" };
ms2.print();
ms1 = ms2; // assignment
ms1.print();
ms2[0] = '?';
ms1.print();
ms2.print();
}
static void testExercise_02()
{
MyStringSP ms1{ "ABCDE" };
ms1.print();
MyStringSP ms2{ ms1 }; // copy construction
ms2.print();
ms2[0] = '?';
ms1.print();
ms2.print();
}
static void testExercise_10()
{
MyStringUP ms1{ "123" };
ms1.print();
MyStringUP ms2{ "ABCDE" };
ms2.print();
ms1 = ms2; // assignment
ms1.print();
ms2[0] = '?';
ms1.print();
ms2.print();
}
static void testExercise_11()
{
MyStringUP ms1{ "ABCDE" };
ms1.print();
MyStringUP ms2{ ms1 }; // copy construction
ms2.print();
ms2[0] = '?';
ms1.print();
ms2.print();
}
static void testExercise_20()
{
MyStringSP ms1{ "ABCDE" };
ms1.print();
MyStringSP ms2{ ms1 }; // copy construction
ms2.print();
ms1 = ms2; // copy assignment
ms1.print();
}
static void testExercise_21()
{
MyStringSP ms1{ "ABCDE" };
ms1.print();
MyStringSP ms2{ std::move(ms1) }; // move construction
ms1.print();
ms2.print();
ms1 = std::move(ms2); // move assignment
ms1.print();
ms2.print();
}
static void testExercise_22()
{
MyStringUP ms1{ "ABCDE" };
ms1.print();
MyStringUP ms2{ ms1 }; // copy construction
ms2.print();
ms1 = ms2; // copy assignment
ms1.print();
}
static void testExercise_23()
{
MyStringUP ms1{ "ABCDE" };
ms1.print();
MyStringUP ms2{ std::move(ms1) }; // move construction
ms1.print();
ms2.print();
ms1 = std::move(ms2); // move assignment
ms1.print();
ms2.print();
}
static void testExercise() {
// default c'tors
testExercise_00();
// MyStringSP
testExercise_01(); // testing assignment
testExercise_02(); // testing copy construction
// MyStringUP
testExercise_10(); // testing assignment
testExercise_11(); // testing copy construction
// MyStringSP
testExercise_20(); // testing copy semantics
testExercise_21(); // testing move semantics
// MyStringUP
testExercise_22(); // testing copy semantics
testExercise_23(); // testing move semantics
}
}
namespace Exercise_05 {
// =========================================================
// helper method 'countDigits'
static std::size_t countDigits(std::size_t n)
{
std::size_t count{};
// do-while ensures that 0 is counted as 1 digit
do {
n /= 10;
++count;
} while (n != 0);
return count;
}
// =========================================================
// Variant 1: return std::unique_ptr directly
// (move semantics and/or copy-move elision behind the scenes)
static std::unique_ptr<std::size_t[]> splitToDigits(
std::size_t number,
std::size_t& count,
bool& success)
{
count = countDigits(number);
std::unique_ptr<std::size_t[]> digits{ std::make_unique<std::size_t[]>(count) };
if (digits == nullptr) {
success = false;
return digits;
}
else {
std::size_t index{};
do {
std::size_t digit = number % 10;
number /= 10;
digits[count - index - 1] = digit;
++index;
} while (number != 0);
success = true;
return digits;
}
}
// =========================================================
// Variant 2: return values by reference parameters
static bool splitToDigitsByRef(
std::size_t number,
std::unique_ptr<std::size_t[]>& digits,
std::size_t& count)
{
count = countDigits(number);
digits = std::make_unique<std::size_t[]>(count);
if (digits == nullptr) {
return false;
}
else {
std::size_t index{};
do {
std::size_t digit = number % 10;
number /= 10;
digits[count - index - 1] = digit;
++index;
} while (number != 0);
return true;
}
}
// =========================================================
// Variant 3: struct
struct DigitsSplitting
{
std::size_t m_number;
std::unique_ptr<std::size_t[]> m_digits;
std::size_t m_count;
bool m_success;
};
static DigitsSplitting splitToDigits(std::size_t number)
{
DigitsSplitting splitting{ number };
auto count{ countDigits(number) };
std::unique_ptr<std::size_t[]> digits{ std::make_unique<std::size_t[]>(count) };
if (digits == nullptr) {
splitting.m_success = false;
return splitting;
}
else {
std::size_t index{};
do {
std::size_t digit = number % 10;
number /= 10;
digits[count - index - 1] = digit;
++index;
} while (number != 0);
splitting.m_digits = std::move(digits);
splitting.m_count = count;
splitting.m_success = true;
return splitting;
}
}
// =========================================================
// Variant 4: std::tuple
using SplittedDigitsTuple =
std::tuple<std::size_t, std::unique_ptr<std::size_t[]>, std::size_t, bool>;
static SplittedDigitsTuple splitToDigitsTuple(std::size_t number)
{
SplittedDigitsTuple splitting{ number, nullptr, 0, false };
auto count{ countDigits(number) };
auto digits{ std::make_unique<std::size_t[]>(count) };
if (digits == nullptr) {
return splitting;
}
else {
std::size_t index{};
do {
std::size_t digit = number % 10;
number /= 10;
digits[count - index - 1] = digit;
++index;
} while (number != 0);
std::get<1>(splitting) = std::move(digits);
std::get<2>(splitting) = count;
std::get<3>(splitting) = true;
return splitting;
}
}
// =========================================================
// Variant 5: std::optional
using SplittedDigitsPair =
std::pair<std::unique_ptr<std::size_t[]>, std::size_t>;
static std::optional<SplittedDigitsPair> splitToDigitsOptional(std::size_t number)
{
std::optional<SplittedDigitsPair> result{};
auto count{ countDigits(number) };
auto digits{ std::make_unique<std::size_t[]>(count) };
if (digits == nullptr) {
return result;
}
else {
std::size_t index{};
do {
std::size_t digit = number % 10;
number /= 10;
digits[count - index - 1] = digit;
++index;
} while (number != 0);
result = std::pair{ std::move(digits) , count };
return result;
}
}
// =========================================================
static void test_returning_unique_ptr_variant_01()
{
// return std::unique_ptr directly
// (Move Semantics or Copy-Move Elision)
std::size_t number{ 12345 };
std::size_t count{};
bool success{};
std::unique_ptr<std::size_t[]> buffer{ splitToDigits(number, count, success) };
if (success) {
std::span<std::size_t> digits{ buffer.get(), count };
std::println("Splitting of {}:", number);
for (std::size_t i{}; auto digit : digits) {
std::println("{}: {}", i, digit);
++i;
}
}
}
static void test_returning_unique_ptr_variant_02()
{
// pass all parameters by reference
std::size_t number{ 54321 };
std::size_t count{};
std::unique_ptr<std::size_t[]> buffer{};
bool success{ splitToDigitsByRef(number, buffer, count) };
if (success) {
std::span<std::size_t> digits{ buffer.get(), count };
std::println("Splitting of {}:", number);
for (std::size_t i{}; auto digit : digits) {
std::println("{}: {}", i, digit);
++i;
}
}
}
static void test_returning_unique_ptr_variant_03()
{
// returning a struct
std::size_t number{ 13524 };
DigitsSplitting splitting{ splitToDigits(number) };
if (splitting.m_success) {
std::span<std::size_t> digits{ splitting.m_digits.get(), splitting.m_count };
std::println("Splitting of {}:", number);
for (std::size_t i{}; auto digit : digits) {
std::println("{}: {}", i, digit);
++i;
}
}
}
static void test_returning_unique_ptr_variant_04()
{
// returning a tuple
std::size_t number{ 53142 };
const auto& [digit, buffer, count, success] { splitToDigitsTuple(number) };
if (success) {
std::span<std::size_t> digits{ buffer.get(), count };
std::println("Splitting of {}:", number);
for (std::size_t i{}; auto digit : digits) {
std::println("{}: {}", i, digit);
++i;
}
}
}
static void test_returning_unique_ptr_variant_05()
{
// returning a std::optional
std::size_t number{ 15243 };
auto result{ splitToDigitsOptional(number) };
if (result.has_value()) {
const auto& [buffer, count] = result.value();
std::span<std::size_t> digits{ buffer.get(), count };
std::println("Splitting of {}:", number);
for (std::size_t i{}; auto digit : digits) {
std::println("{}: {}", i, digit);
++i;
}
}
std::println();
}
// =========================================================
void testExercise()
{
test_returning_unique_ptr_variant_01();
test_returning_unique_ptr_variant_02();
test_returning_unique_ptr_variant_03();
test_returning_unique_ptr_variant_04();
test_returning_unique_ptr_variant_05();
}
}
namespace Exercise_06 {
class Child; // Forward declaration
class Mom {
private:
std::string m_name;
std::weak_ptr<const Child> m_child;
public:
explicit Mom(std::string name)
: m_name(std::move(name))
{}
~Mom() {
std::println("Mother ({}) passes away.", m_name);
}
const std::string& getName() const { return m_name; }
void setChild(const std::shared_ptr<Child>& child) {
m_child = child;
}
void sayHello() const;
};
class Child {
private:
std::string m_name;
std::weak_ptr<const Mom> m_mother;
public:
explicit Child(std::string name)
: m_name(std::move(name))
{}
~Child() {
std::println("Child ({}) passes away.", m_name);
}
const std::string& getName() const { return m_name; }
void setMother(const std::shared_ptr<Mom>& mother) {
m_mother = mother;
}
void sayHello() const {
if (auto mother = m_mother.lock()) {
std::println("{}: Hello mother.", m_name);
}
else {
std::println("{}: My mother no longer exists.", m_name);
}
}
};
void Mom::sayHello() const {
if (auto child = m_child.lock()) {
std::println("{}: Hello child.", m_name);
}
else {
std::println("{}: My child no longer exists.", m_name);
}
}
static void testExercise() {
using namespace Exercise_06;
// both human beings are independently owned
auto mother = std::make_shared<Mom>("Dorothea");
auto child = std::make_shared<Child>("John");
// establish the relationships
mother->setChild(child);
child->setMother(mother);
std::println("{}: use_count = {}", mother->getName(), mother.use_count());
std::println("{}: use_count = {}", child->getName(), child.use_count());
// living independently
mother->sayHello();
child->sayHello();
// first human being is passing away
bool motherPassesFirst = true;
if (motherPassesFirst) {
// mother no longer exists
mother.reset();
child->sayHello();
}
else {
// mother no longer exists
child.reset();
mother->sayHello();
}
}
}
}