aboutsummaryrefslogtreecommitdiff
path: root/week06/Exercise02.cpp
blob: 63daeac0f5398edb6e989f47cf02399c545a258d (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
#include <cstring>

struct DynamicString {
private:
	char* str;
	unsigned size;

	void free() {
		delete[] str;
	}
	void copyFrom(const DynamicString& other) {
		this->str = new char[other.size];
		strcpy(this->str, other.str);
		this->size = other.size;
	}

public:
	DynamicString() {
		str = nullptr;
		size = 0;
	}
	~DynamicString() {
		free();
	}
	DynamicString(const DynamicString& other) {
		copyFrom(other);
	}
	DynamicString& operator=(const DynamicString& other) {
		if (this != &other) {
			free();
			copyFrom(other);
		}
		return *this;
	}
};