using namespace std;
template <class T> class Array;
template <class T> class ArrayBody
{
friend class Array<T>;
T* tpBody;
int iRows, iColumn, iCurrentRow;
ArrayBody(int iRsz, int iCsz)
{
tpBody = new T[iRsz * iRsz];
iRows = iRsz;
iColumn = iCsz;
iCurrentRow = -1;
}
public:
T& operator[](int j)
{
bool row_error =false, column_error =false;
try
{
if(iCurrentRow < 0 || iCurrentRow >= iRows)
row_error =true;
if(j < 0 || j >= iColumn)
column_error =true;
if(row_error || column_error)
throw 'e';
}
catch (char)
{
if(row_error)
cerr << "row access invalid!" << iCurrentRow << endl;
if(column_error)
cerr << "column access invalid!" << j << endl;
}
return tpBody[iCurrentRow * iColumn + j];
}
~ArrayBody()
{delete[] tpBody;}
};
template <class T> class Array
{
ArrayBody<T> tBody;
public:
ArrayBody<T>& operator[](int i)
{
tBody.iCurrentRow = i;
return tBody;
}
Array(int iRsz, int iCsz): tBody(iRsz, iCsz){}
};
void main()
{
Array<int> a1(10,20);
Array<double> a2(3,5);
int b1;
double b2;
b1 = a1[-5][10];
b1 = a1[10][15];
b1 = a1[1][4];
b2 = a2[2][6];
b2 = a2[10][20];
b2 = a2[1][4];
getchar();
}