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:
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:
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:
perator[](int i){
return arrayPtr;
}
const Array &TwoDArray:
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
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:
{
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:
{
// 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:
return arrayPtr;
}
const Array &TwoDArray:
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