summaryrefslogtreecommitdiffstats
path: root/labb4/test-rule-of-three/main.cpp
blob: ccef31a35126ea7ce961be8f63498f0824b223d6 (plain) (blame)
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
#include <iostream>
#include "main.hpp"

Base *DerivedA::clone() {
    std::cout << "clone da" << std::endl;
    return new DerivedA;
}

Base *DerivedB::clone() {
    std::cout << "clone db" << std::endl;
    return new DerivedB;
}

void Base::hello() {
    std::cout << 0 << std::endl;
}

void DerivedA::hello() {
    std::cout << 1 << std::endl;
}

void DerivedB::hello() {
    std::cout << 2 << std::endl;
}

GameState::GameState() {
    std::cout << "c" << std::endl;
    thing = new DerivedA;
}

GameState::GameState(const GameState &other) {
    std::cout << "cc" << std::endl;
    thing = other.thing->clone();
}

GameState &GameState::operator=(const GameState &other) {
    std::cout << "=" << std::endl;
    thing = other.thing;
    return *this;
}

GameState::~GameState() {
    std::cout << "d gamestate" << std::endl;
    delete thing;
    thing = nullptr;
}

void GameState::switchThing() {
    std::cout << "switch" << std::endl;
    delete thing;
    thing = new DerivedB;
}

int main(int argc, char *argv[]) {
    GameState state;
    state.thing->hello();
    std::cout << std::endl;

    GameState copy = state;
    state.thing->hello();
    copy.thing->hello();
    std::cout << std::endl;

    copy.switchThing();
    state.thing->hello();
    copy.thing->hello();
    std::cout << std::endl;

    state = copy;
    state.thing->hello();
    copy.thing->hello();

    return 0;
}