Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to overload operator[] for use in 2d array class

Status
Not open for further replies.

mrampton

Programmer
Jan 11, 2001
7
US
Hi.

I'm trying to figure out how to overload operator[] for use in a 2d array of ints class: TwoDArray.

I need to be able to access the array data using normal 2d array notation like this:

TwoDArray temp; // constructors set to temp[10][10]
temp[5][6] = 2;

My TwoDArray class inherits a 1d array class (Array -- from Deitel & Deitels "How to Program C++") and sets up an Array of Arrays.

The Array class itself overloads operator[] in the following manner:

int &Array::eek:perator[]( int subscript )
{
if ( subscript < 0 || subscript >= size ) {
cout << "\nError: Subscript " << subscript
<< " out of range" << endl;
exit( 1 ); // terminate program; subscript out of range
} // end if
return ptr[ subscript ]; // reference return
} // end function operator[]

// overloaded subscript operator for const Arrays
// const reference return creates an rvalue
const int &Array::eek:perator[]( int subscript ) const
{
// check for subscript out of range error
if ( subscript < 0 || subscript >= size ) {
cout << "\nError: Subscript " << subscript
<< " out of range" << endl;
exit( 1 ); // terminate program; subscript out of range
} // end if
return ptr[ subscript ]; // const reference return
} // end function operator[]

What I don't understand is how I'm supposed to overload them for use as TwoDArray temp[r][c]= x;

I've tried adding the following:

Array &TwoDArray::eek:perator[](int i){
return arrayPtr;
}

const Array &TwoDArray::eek:perator[](int i) const{
return arrayPtr;
}

This is my class header:

class TwoDArray: public Array {
public:
TwoDArray();
TwoDArray(int nRows, int nCols);
~TwoDArray();
Array &operator[](int);
const Array &operator[](int) const;
private:
int rows;
Array *arrayPtr;
};

Does anyone have any advice on what direction I should head on this? Thanks for the help!

-mark
 
You accomplish it by letting the [] operator of the TwoDArray return something that also has an [] operator.

Ie the
Code:
Array &TwoDArray::operator[](int i){
    return arrayPtr[i];
}

const Array &TwoDArray::operator[](int i) const{
    return arrayPtr[i];
}

solution should work, given that Array has the corresponding [] operators.

Oh, and rather than re-inventing the wheel, may I suggest you look into the std::vector class of STL (part of C++ standard). It handles arrays amazingly well...

Since it is so generic in its nature, defining a 2d array is trivial:
Code:
typedef std::vector<std::vector<int> > TwoDArray;


/Per

&quot;It was a work of art, flawless, sublime. A triumph equaled only by its monumental failure.&quot;
 
Perf,

Thanks. You're right it does work. I was just staring at it too long and had my client testing it out right before i overwrote the entire array with clean data (ugh...).

Thanks for the info on std::vector as well.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top