// 2D dynamic array in C++. Adapted from an idea by James Kanze // posted on comp.lang.c++.moderated 11/23/00. // Written by Wayne Pollock, Tampa Florida USA, 11/2000 #include using namespace std; template < typename T > class Array2D { public: Array2D( int r , int c ) : rowSize( r ), rep( new T[ r * c ] ) {} ~Array2D() { delete [] rep; } T* operator[] ( int i ) { return rep + i * rowSize; } private: int rowSize; T* rep; }; int main () { Array2D foo(24, 75); foo[2][3] = '*'; char x = foo[2][3]; cout << "x = " << x << ", foo[2][3] = " << foo[2][3] << endl; }
Send comments and mail to the WebMaster. |