#include "stdafx.h"
#include <iostream>
using namespace std;
template <class T>
class MyArray
{
int len;
public:
T *data;
MyArray()
{
data = NULL;
len = 0;
}
~MyArray()
{
delete[] data;
}
T& operator [](int index);
void push(T d);
};
template <class T>
T& MyArray<T>::operator [](int index)
{
if(index<0||index>(len-1))
{
cout<<"Bad subscript!"<<endl;
exit(1);
}
return data[index];
}
template <class T>
void MyArray<T>::push(T d)
{
T *pdata = data;
data = new T[len + 1];
if(pdata != NULL)
{
for(int i = 0 ; i < len ; i++)
{
data[i] = pdata[i];
}
delete[] pdata;
}
data[len] = d;
len++;
}
//測試代碼
int main(int argc, char* argv[])
{
MyArray<int> a;
a.push(11);
a.push(22);
a.push(33);
a.push(55);
a[0]=44;
cout<<a[0]<<endl<<a[1]<<endl<<a[2]<<endl<<a[3]<<endl;
return 0;
}