blob: 0686a2c1330c2b2785ede15a9a00a3527469c66e (
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
76
77
78
79
80
|
#include "Exercise1.h"
#include <cstring>
void StreetList::free() {
for (int i = 0; i < lastUnused; i++) {
delete[] streetNames[i];
}
delete[] streetNames;
}
void StreetList::copyFrom(const StreetList& other) {
this->lastUnused = other.lastUnused;
this->allocated = other.allocated;
this->streetNames = new char*[allocated];
for (int i = 0; i < lastUnused; i++) {
this->streetNames[i] = new char[strlen(other.streetNames[i]) + 1];
strcpy(this->streetNames[i], other.streetNames[i]);
}
}
void StreetList::resize() {
allocated *= 2;
char** biggerList = new char*[allocated];
for (int i = 0; i < lastUnused; i++) {
// Понеже са указатели в динамичната памет, можем да си спестим new char[] и strcpy
biggerList[i] = streetNames[i];
}
delete[] streetNames;
streetNames = biggerList;
}
StreetList::StreetList() {
streetNames = nullptr;
lastUnused = allocated = 0;
}
StreetList::~StreetList() {
free();
}
StreetList::StreetList(const StreetList& other) {
copyFrom(other);
}
StreetList& StreetList::operator=(const StreetList& other) {
if (this != &other) {
free();
copyFrom(other);
}
return *this;
}
StreetList::StreetList(StreetList&& other) {
this->streetNames = other.streetNames;
other.streetNames = nullptr;
this->lastUnused = other.lastUnused;
this->allocated = other.allocated;
}
StreetList& StreetList::operator=(StreetList&& other) {
if (this != &other) {
free();
this->streetNames = other.streetNames;
other.streetNames = nullptr;
this->lastUnused = other.lastUnused;
this->allocated = other.allocated;
}
return *this;
}
void StreetList::Add(const char* newString) {
if (lastUnused == allocated) {
resize();
}
streetNames[lastUnused] = new char[strlen(newString) + 1];
strcpy(streetNames[lastUnused], newString);
lastUnused++;
}
|