diff options
| author | Miguel <m.i@gmx.at> | 2025-01-09 12:39:13 +0100 |
|---|---|---|
| committer | Miguel <m.i@gmx.at> | 2025-01-09 12:39:13 +0100 |
| commit | ef4538478075dada9161c1f0175ae7409f977603 (patch) | |
| tree | 7783898d21161c0c8b2ef64cb24d1492e846d385 /test.cpp | |
| parent | 6d7e26adf0d090038cd2dae6c6e4fa93e55fc575 (diff) | |
building on win
Diffstat (limited to 'test.cpp')
| -rw-r--r-- | test.cpp | 81 |
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; + +} |
