-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplated-array.h
55 lines (44 loc) · 941 Bytes
/
templated-array.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
#ifndef ARRAY_H
#define ARRAY_H
#include <assert.h> // for assert()
template <class T>
class Array
{
private:
int m_length;
T *m_data;
public:
Array()
{
m_length = 0;
m_data = nullptr;
}
Array(int length)
{
m_data = new T[length];
m_length = length;
}
~Array()
{
delete[] m_data;
}
void Erase()
{
delete[] m_data;
// We need to make sure we set m_data to 0 here, otherwise it will
// be left pointing at deallocated memory!
m_data = nullptr;
m_length = 0;
}
T& operator[](int index)
{
assert(index >= 0 && index < m_length);
return m_data[index];
}
// The length of the array is always an integer
// It does not depend on the data type of the array
int getLength();
};
template <typename T>
int Array<T>::getLength() { return m_length; }
#endif