summaryrefslogtreecommitdiff
path: root/test.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'test.cpp')
-rw-r--r--test.cpp81
1 files changed, 81 insertions, 0 deletions
diff --git a/test.cpp b/test.cpp
new file mode 100644
index 0000000..b987af9
--- /dev/null
+++ b/test.cpp
@@ -0,0 +1,81 @@
+// Some stupid experiments as I forgot C++
+
+#include <cstdlib>
+#include <iostream>
+#include <string>
+#include <utility>
+#include <initializer_list>
+
+class C1 {
+
+public:
+
+ C1() : name{"default"} {
+ std::cout << "C1 default cons: " << name << std::endl;
+ }
+
+ C1(const std::string& str) : name{str} {
+ std::cout << "C1 cons: " << name << std::endl;
+ }
+
+ C1(std::initializer_list<std::string> list) {
+ for (std::string val : list) name+=val;
+ std::cout << "C1 initializer_list cons: " << name << std::endl;
+ }
+
+ ~C1() {
+ std::cout << "C1 decons: " << name << std::endl;
+ }
+
+ C1(C1&& other) : name{other.name} {
+ std::cout << "C1 move constructor: " << name << std::endl;
+ other.name+=" (moved away)";
+ }
+
+ C1& operator=(C1&& other) {
+ std::cout << "C1 move called: " << other.name << " -> " << name << std::endl;
+ name = other.name;
+ other.name+=" (moved away)";
+ return *this;
+ }
+
+ // Disable copy and copy-assign
+ C1(const C1&) = delete;
+ C1& operator=(const C1&) = delete;
+
+ void rename (const std::string &str){
+ name = str;
+ }
+
+ void say() {
+ std::cout << "C1 says: " << name << std::endl;
+ }
+
+private:
+
+ std::string name;
+
+};
+
+
+int main() {
+
+ std::cout << "Miguel's C++ Tests" << std::endl;
+
+ C1 a ("x");
+ C1 b (std::move(a));
+
+ std::cout << "--" << std::endl;
+ a.say();
+ b.say();
+ a.rename("y");
+ a.say();
+ b.say();
+ std::cout << "--" << std::endl;
+
+ C1 c{"c","d"};
+ c.say();
+
+ return EXIT_SUCCESS;
+
+}