یک مثال ساده :
#include <iostream>
using namespace std;
class Mat {
public:
Mat(int row,int col) :
row_(row),
col_(col)
{
mat_ = new int*[row_];
for(int i = 0; i < row_; ++i)
mat_[i] = new int[col_];
for(int i=0;i<row_;i++)
for(int j=0;j<col_;j++)
{
mat_[i][j]=i+j;
}
}
/* To do
* copy constructor
* ~Mat()
* operator =
*/
class Indexer
{
public:
Indexer(int* arr_,int col) :
arr_(arr_),
col_(col)
{}
int operator[](int index)
{
if(index>=col_)
throw "Out of cols index";
return arr_[index];
}
private:
int* arr_;
int col_;
};
Indexer operator[](int index) {
if(index>=row_)
throw "Out of row index";
return Indexer(mat_[index],col_);
}
private:
int **mat_;
int row_;
int col_;
};
int main()
{
Mat arr(70,7);
try
{
cout<<arr[52][2]<<"\n";
cout<<arr[52][22];
}
catch (const char* exc)
{
cout<<exc;
}
}