스터디/C++
[ C++ ] 배열 클래스의 템플릿화
by 알 수 없는 사용자
2019. 11. 30.
#ifndef __ARRAY_TEMPLATE_H_
#define __ARRAY_TEMPLATE_H_
#include <iostream>
#include <cstring>
using namespace std;
template <typename T>
class Bound_Chk_Array
{
private:
T * arr;
int len;
Bound_Chk_Array(const Bound_Chk_Array& arr) { }
Bound_Chk_Array& operator=(const Bound_Chk_Array& arr) { }
public:
Bound_Chk_Array(int _len);
T& operator[] (int idx);
T operator[] (int idx) const;
int GetArrLen() const;
~Bound_Chk_Array();
};
template <typename T>
Bound_Chk_Array<T>::Bound_Chk_Array(int _len) : len(_len)
{
arr = new T[len];
}
template <typename T>
T& Bound_Chk_Array<T>::operator[] (int idx)
{
if (idx < 0 || idx >= len)
{
cout << "Array index out of bound exception" << endl;
exit(1);
}
return arr[idx];
}
template <typename T>
T Bound_Chk_Array<T>::operator[] (int idx) const
{
if (idx < 0 || idx >= len)
{
cout << "Array index out of bound exception" << endl;
exit(1);
}
return arr[idx];
}
template <typename T>
int Bound_Chk_Array<T>::GetArrLen() const
{
return len;
}
template <typename T>
Bound_Chk_Array<T>::~Bound_Chk_Array()
{
delete[] arr;
}
#endif