summaryrefslogtreecommitdiff
path: root/test.cpp
blob: cf2830cc28198213bdb13fdac9c62fc2562d7f0a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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;

}