template <typename T>
class BoundCheckArray
{
private:
T * arr;
int arrlen;
BoundCheckArray(const BoundCheckArray& arr) { }
BoundCheckArray& operator=(const BoundCheckArray& arr) { }
public:
BoundCheckArray(int len); // 생성자
template <typename T>
BoundCheckArray<T>::BoundCheckArray(int len) :arrlen(len)
{
arr=new T[len];
}
T& operator[] (int idx); // 배열 연산자
template <typename T>
T& BoundCheckArray<T>::operator[] (int idx)
{
if(idx<0 || idx>=arrlen)
{
cout<<"Array index out of bound exception"<<endl;
exit(1);
}
return arr[idx];
}
T operator[] (int idx) const; //const 배열 연산자
template <typename T>
T BoundCheckArray<T>::operator[] (int idx) const
{
if(idx<0 || idx>=arrlen)
{
cout<<"Array index out of bound exception"<<endl;
exit(1);
}
return arr[idx];
}
int GetArrLen() const;
template <typename T>
int BoundCheckArray<T>::GetArrLen() const
{
return arrlen;
}
~BoundCheckArray(); //소멸자
BoundCheckArray<T>::~BoundCheckArray()
{
delete []arr;
}
int main(void)
{
/*** int형 정수 저장 ***/
BoundCheckArray<int> iarr(5);
for(int i=0; i<5; i++)
iarr[i]=(i+1)*11;
for(int i=0; i<5; i++)
cout<<iarr[i]<<endl;
/*** Point 객체 저장 ***/
BoundCheckArray<Point> oarr(3);
oarr[0]=Point(3, 4);
oarr[1]=Point(5, 6);
oarr[2]=Point(7, 8);
for(int i=0; i<oarr.GetArrLen(); i++)
cout<<oarr[i];
/*** Point 객체의 주소 값 저장 ***/
typedef Point * POINT_PTR;
BoundCheckArray<POINT_PTR> parr(3);
parr[0]=new Point(3, 4);
parr[1]=new Point(5, 6);
parr[2]=new Point(7, 8);
for(int i=0; i<parr.GetArrLen(); i++)
cout<<*(parr[i]);
delete parr[0];
delete parr[1];
delete parr[2];
return 0;
}
객체의 TEMPLATE 형태
기존 형태
smartTEMP <int> iarr(50);
객체 저장 형태
smartTEMP <Point<int>> iarr(50);
객체포인터 저장 형태
smartTEMP <Point<int>*> iarr(50);
typedef 선언을 통한 저장 형태
typedef <Point<int>*> POINT_PTR
smartTEMP <POINT_PTR> iarr(50);
일반함수에서 템플릿 클래스 객체를 인자로 받을수 있다.
외부에 선언된 함수를 내부에서 자유롭게 사용하기 위해서 friend 를 선언해 준다!!
PointTemplateFriendFunction.cpp
public:
Point(T x=0, T y=0): xpos(x), ypos(y)
{ }
void ShowPosition() const
{
cout<<'['<<xpos<<", "<<ypos<<']'<<endl;
}
friend Point<int> operator+(const Point<int>&, const Point<int>&);
friend ostream& operator<<(ostream& os, const Point<int>& pos);
};
================================================================A R M==================================================================
========================================================================================================================================
'코스웨어 > 14년 스마트컨트롤러' 카테고리의 다른 글
ARM (0) | 2014.11.05 |
---|---|
2014.11.04. 업무일지 22번 허수웅 (8) | 2014.11.05 |
STM32F103ZE (3) | 2014.11.04 |
2014.11.03 업무일지 출석번호21 이재우 (9) | 2014.11.04 |
ARM_PIT_LED 손병규 (0) | 2014.10.31 |
ARM Interval Timer LED on, off 김해성 (1) | 2014.10.31 |
ADS LED ON/OFF(PIT) 김진철 (0) | 2014.10.31 |
일일보고서 손병규 - 20141030 (6) | 2014.10.31 |