-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArrayWrapper.h
77 lines (64 loc) · 1.28 KB
/
ArrayWrapper.h
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
#ifndef ARRAYWRAPPER_H_
#define ARRAYWRAPPER_H_
template <typename T>
class ArrayWrapper
{
private:
T* internal;
unsigned int length;
public:
const static int INVALID_ARGUMENT_ERR = 1;
ArrayWrapper(T* array, unsigned int length, bool deleteOld = false)
{
this->internal = new T[length];
this->length = length;
for (unsigned int i = 0; i < this->length; i++)
{
this->internal[i] = array[i];
}
if (deleteOld)
{
delete[] array;
}
}
ArrayWrapper(const ArrayWrapper& copy)
{
internal = new T[copy.length];
length = copy.length;
for (unsigned int i = 0; i < length; i++)
{
internal[i] = copy.internal[i];
}
}
~ArrayWrapper()
{
delete[] internal;
}
ArrayWrapper& operator=(const ArrayWrapper& copy)
{
if (this != ©)
{
delete[] internal;
internal = new T[copy.length];
length = copy.length;
for (unsigned int i = 0; i < length; i++)
{
internal[i] = copy.internal[i];
}
}
return *this;
}
T& operator[](const unsigned int index)
{
if (index < 0 || index >= length)
{
throw INVALID_ARGUMENT_ERR;
}
return internal[index];
}
unsigned int arrayLength()
{
return length;
}
};
#endif /* ARRAYWRAPPER_H_ */