aboutsummaryrefslogtreecommitdiff
path: root/week13/Exercise2/Container.hpp
blob: 5d6876da1debcedaba3c0c0106bdb501c3e64329 (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
75
#pragma once

template <class T>
class Container {
	void free();
	void copyFrom(const Container& other);

protected:
	T* arr;
	unsigned size;

public:
	Container();
	virtual ~Container();
	Container(const Container& other);
	Container& operator=(const Container& other);
	Container(Container&& other);
	Container& operator=(Container&& other);
};

template <class T>
void Container<T>::free() {
	delete[] arr;
}

template <class T>
void Container<T>::copyFrom(const Container& other) {
	this->size = other.size;
	this->arr = new T[size];
	for (int i = 0; i < size; i++) {
		this->arr[i] = other.arr[i];
	}
}

template <class T>
Container<T>::Container() {
	this->arr = nullptr;
	size = 0;
}

template <class T>
Container<T>::~Container() {
	free();
}

template <class T>
Container<T>::Container(const Container<T>& other) {
	copyFrom(other);
}

template <class T>
Container<T>& Container<T>::operator=(const Container<T>& other) {
	if (this != &other) {
		free();
		copyFrom(other);
	}
	return *this;
}

template <class T>
Container<T>::Container(Container<T>&& other) {
	this->arr = other.arr;
	other.arr = nullptr;
}

template <class T>
Container<T>& Container<T>::operator=(Container<T>&& other) {
	if (this != &other) {
		free();

		this->arr = other.arr;
		other.arr = nullptr;
	}
	return *this;
}