commit 05b8b7d3239bc53955a8ec8e7d9991224c0fd917
parent c5bedb79e7c808294e85ed6fbba9aae527cd2990
Author: Sebastiano Tronto <sebastiano@tronto.net>
Date: Thu, 26 Dec 2024 12:19:10 +0100
Added RAII
Diffstat:
6 files changed, 138 insertions(+), 0 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -1 +1,2 @@
sort-benchmark/*/sort
+*/a.out
diff --git a/README.md b/README.md
@@ -4,6 +4,9 @@ Experiments with C++ and comparisons with C.
## sort-benchmark
+For the
+[first post in the series](https://sebastiano.tronto.net/blog/2024-04-30-taming-cpp-motivation/).
+
Inspired by [Bert's post](https://berthub.eu/articles/posts/c++-1).
Sorting an array of integers is a simple task, so C and C++ should perform
@@ -17,3 +20,7 @@ C++ time for 100000000 numbers (parallel): 0.495435s
A similar benchmark is provided for sorting an arrays of pairs in a
non-standard way.
+
+## raii
+
+Various small examples on constructors, destructors and related things.
diff --git a/raii/all-constructors.cpp b/raii/all-constructors.cpp
@@ -0,0 +1,94 @@
+#include <iostream>
+
+class T {
+public:
+ T(const std::string& a) : name{a} {
+ std::cout << "Called constructor for " << name
+ << " (" << this << ")" << std::endl;
+ }
+
+ ~T() {
+ std::cout << "Called destructor for " << name
+ << " (" << this << ")" << std::endl;
+ }
+
+ T(T& other) : name{other.name} {
+ std::cout << "Copy constructor called for " << name
+ << " (" << this << " copied from "
+ << &other << ")" << std::endl;
+ }
+
+ T& operator=(T& other) {
+ name = other.name;
+ std::cout << "Copy assignment called for " << name
+ << " (" << this << " copied from "
+ << &other << ")" << std::endl;
+ return *this;
+ }
+
+ T(T&& other) : name{other.name} {
+ std::cout << "Move constructor called for " << name
+ << " (" << this << " moved from "
+ << &other << ")" << std::endl;
+ }
+
+ T& operator=(T&& other) {
+ name = other.name;
+ std::cout << "Move assignment called for " << name
+ << " (" << this << " moved from "
+ << &other << ")" << std::endl;
+ return *this;
+ }
+private:
+ std::string name;
+};
+
+T do_nothing_and_return(T x) {
+ return x;
+}
+
+T create(const std::string& str) {
+ return T(str);
+}
+
+int main() {
+ //T bad; // Compile-time error: no default constructor.
+
+ std::cout << "---------- Declaring a simple variable ----------" << std::endl;
+ T a("A");
+ std::cout << std::endl;
+
+ // Now we can see that objects are indeed scope-bound: they
+ // are destroyed when they go out of scope.
+ std::cout << "--- Declaring a variable in a temporary scope ---" << std::endl;
+ {
+ std::cout << "Entering temporary scope" << std::endl;
+ T b("short-lived");
+ std::cout << "Exiting temporary scope" << std::endl;
+ }
+ std::cout << "Out of temporary scope" << std::endl;
+ std::cout << std::endl;
+
+ std::cout << "------------ Constructing by copying ------------" << std::endl;
+ T b("B");
+ T c(b);
+ T d = b; // This is also a copy constructor, not a copy assignment
+ std::cout << std::endl;
+
+ // Copy assignment operators differ from copy constructors in that they
+ // should also clean up the resources for the copied-to object.
+ std::cout << "---------------- Copy assignment ----------------" << std::endl;
+ c = b;
+ std::cout << std::endl;
+
+ std::cout << "------------ Constructing by moving -------------" << std::endl;
+ T e(do_nothing_and_return(b));
+ std::cout << std::endl;
+
+ std::cout << "---------------- Move assignment ----------------" << std::endl;
+ e = create("E");
+ std::cout << std::endl;
+
+ std::cout << "------------- Destroying everything -------------" << std::endl;
+ return 0;
+}
diff --git a/raii/allocate.cpp b/raii/allocate.cpp
@@ -0,0 +1,8 @@
+bool f(int big_number) {
+ int *a = new int[big_number];
+ return true;
+}
+
+int main() {
+ return 0;
+}
diff --git a/raii/constructor-hello-world.cpp b/raii/constructor-hello-world.cpp
@@ -0,0 +1,17 @@
+#include <iostream>
+
+class BadType {
+public:
+ BadType() {
+ std::cout << "Variable created!" << std::endl;
+ }
+
+ ~BadType() {
+ std::cout << "Variable destroyed, bye bye" << std::endl;
+ }
+};
+
+int main() {
+ BadType x;
+ return 0;
+}
diff --git a/raii/surprising-error.cpp b/raii/surprising-error.cpp
@@ -0,0 +1,11 @@
+class BadType {
+public:
+ BadType() {
+ BadType recursion_hell;
+ }
+};
+
+int main() {
+ BadType x;
+ return 0;
+}