aboutsummaryrefslogtreecommitdiff
path: root/raii/all-constructors.cpp
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--raii/all-constructors.cpp94
1 files changed, 94 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}

Generated with cgit - Back to sebastiano.tronto.net