aboutsummaryrefslogtreecommitdiff
path: root/raii
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano@tronto.net>2024-12-26 12:19:10 +0100
committerSebastiano Tronto <sebastiano@tronto.net>2024-12-26 12:19:10 +0100
commit05b8b7d3239bc53955a8ec8e7d9991224c0fd917 (patch)
tree7056e26d635cefa7341b6cda9af3e8061f7e708b /raii
parentc5bedb79e7c808294e85ed6fbba9aae527cd2990 (diff)
downloadtaming-cpp-05b8b7d3239bc53955a8ec8e7d9991224c0fd917.tar.gz
taming-cpp-05b8b7d3239bc53955a8ec8e7d9991224c0fd917.zip
Added RAII
Diffstat (limited to 'raii')
-rw-r--r--raii/all-constructors.cpp94
-rw-r--r--raii/allocate.cpp8
-rw-r--r--raii/constructor-hello-world.cpp17
-rw-r--r--raii/surprising-error.cpp11
4 files changed, 130 insertions, 0 deletions
diff --git a/raii/all-constructors.cpp b/raii/all-constructors.cpp
new file mode 100644
index 0000000..27add07
--- /dev/null
+++ b/raii/all-constructors.cpp
@@ -0,0 +1,94 @@
1#include <iostream>
2
3class T {
4public:
5 T(const std::string& a) : name{a} {
6 std::cout << "Called constructor for " << name
7 << " (" << this << ")" << std::endl;
8 }
9
10 ~T() {
11 std::cout << "Called destructor for " << name
12 << " (" << this << ")" << std::endl;
13 }
14
15 T(T& other) : name{other.name} {
16 std::cout << "Copy constructor called for " << name
17 << " (" << this << " copied from "
18 << &other << ")" << std::endl;
19 }
20
21 T& operator=(T& other) {
22 name = other.name;
23 std::cout << "Copy assignment called for " << name
24 << " (" << this << " copied from "
25 << &other << ")" << std::endl;
26 return *this;
27 }
28
29 T(T&& other) : name{other.name} {
30 std::cout << "Move constructor called for " << name
31 << " (" << this << " moved from "
32 << &other << ")" << std::endl;
33 }
34
35 T& operator=(T&& other) {
36 name = other.name;
37 std::cout << "Move assignment called for " << name
38 << " (" << this << " moved from "
39 << &other << ")" << std::endl;
40 return *this;
41 }
42private:
43 std::string name;
44};
45
46T do_nothing_and_return(T x) {
47 return x;
48}
49
50T create(const std::string& str) {
51 return T(str);
52}
53
54int main() {
55 //T bad; // Compile-time error: no default constructor.
56
57 std::cout << "---------- Declaring a simple variable ----------" << std::endl;
58 T a("A");
59 std::cout << std::endl;
60
61 // Now we can see that objects are indeed scope-bound: they
62 // are destroyed when they go out of scope.
63 std::cout << "--- Declaring a variable in a temporary scope ---" << std::endl;
64 {
65 std::cout << "Entering temporary scope" << std::endl;
66 T b("short-lived");
67 std::cout << "Exiting temporary scope" << std::endl;
68 }
69 std::cout << "Out of temporary scope" << std::endl;
70 std::cout << std::endl;
71
72 std::cout << "------------ Constructing by copying ------------" << std::endl;
73 T b("B");
74 T c(b);
75 T d = b; // This is also a copy constructor, not a copy assignment
76 std::cout << std::endl;
77
78 // Copy assignment operators differ from copy constructors in that they
79 // should also clean up the resources for the copied-to object.
80 std::cout << "---------------- Copy assignment ----------------" << std::endl;
81 c = b;
82 std::cout << std::endl;
83
84 std::cout << "------------ Constructing by moving -------------" << std::endl;
85 T e(do_nothing_and_return(b));
86 std::cout << std::endl;
87
88 std::cout << "---------------- Move assignment ----------------" << std::endl;
89 e = create("E");
90 std::cout << std::endl;
91
92 std::cout << "------------- Destroying everything -------------" << std::endl;
93 return 0;
94}
diff --git a/raii/allocate.cpp b/raii/allocate.cpp
new file mode 100644
index 0000000..519f09f
--- /dev/null
+++ b/raii/allocate.cpp
@@ -0,0 +1,8 @@
1bool f(int big_number) {
2 int *a = new int[big_number];
3 return true;
4}
5
6int main() {
7 return 0;
8}
diff --git a/raii/constructor-hello-world.cpp b/raii/constructor-hello-world.cpp
new file mode 100644
index 0000000..c86e1ca
--- /dev/null
+++ b/raii/constructor-hello-world.cpp
@@ -0,0 +1,17 @@
1#include <iostream>
2
3class BadType {
4public:
5 BadType() {
6 std::cout << "Variable created!" << std::endl;
7 }
8
9 ~BadType() {
10 std::cout << "Variable destroyed, bye bye" << std::endl;
11 }
12};
13
14int main() {
15 BadType x;
16 return 0;
17}
diff --git a/raii/surprising-error.cpp b/raii/surprising-error.cpp
new file mode 100644
index 0000000..b2e7831
--- /dev/null
+++ b/raii/surprising-error.cpp
@@ -0,0 +1,11 @@
1class BadType {
2public:
3 BadType() {
4 BadType recursion_hell;
5 }
6};
7
8int main() {
9 BadType x;
10 return 0;
11}

Generated with cgit - Back to sebastiano.tronto.net