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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

overload [] for class to work like an array

Status
Not open for further replies.

ankursaxena

Programmer
Sep 7, 2001
133
US
how would you overload [] for your class to work like an array??

thanx in advance

cheers,
Ankur
 
It has to be a member function:

Code:
return_type ClassName::operator[]( int index );

The return_type could be a reference or const reference depending on what's going on exactly. Given the index, find the item needed and return it.
 
typically returns by const refrence, and you may want it to throw a invalid_argument or out_of_range error, and take an unsigned int (std::size_t), unless you're doing something "unique" with it. If the class has a container as a private data member and you want to wrap it so that your class uses it's random access, then do this:

type& class::eek:perator[](const unsigned int index&)
{
return container[index];
}

 
An example.

#include <iostream>
using namespace std;
class ArrayObject
{
int *array;
int arrayLength;
public :
ArrayObject(int i)
{
arrayLength = i;
array = new int[10];
}
int & operator [] (int index)
{
if(index >= 0 && index < arrayLength)
{
return array[index];
}
cout << &quot;Its after the if statement&quot; << endl;
throw &quot;index out off array length&quot;;
}
~ArrayObject()
{
delete [] array;
}
};

int main()
{
ArrayObject a(10);
int i;
for(i = 0; i < 10; i++)
{
a = (i + 1) * 100;
}
try
{
a[11];
}
catch(char *t)
{
cout << t << endl;
cout.flush();
}
for(i = 0; i < 10; i++)
{
cout << i << &quot; : &quot; << a << endl;
cout.flush();
}
return 0;
}
 
smaniraja, great post, but that would generate a warning about reaching end of non-void function operator[], because the throw isn't a return. Two work around would be switch the throw and the return. Also, you may want to throw anexception and promise it in the signature.

int & operator [] (unsigned int index) throw(out_of_range)
{
if(index < 0 && index >= arrayLength)
throw out_of_range(&quot;index out of array range&quot;);
return array[index];
}

I know, I am nit picking. :( sorry.
 
the problem here is even after throw the function is not returning and its cont. to access the next line which gives an abnormal exception and exits the program, this is what i wanted to get away from.

if someone can tell me how that woul dbe great thanx

Ankur
 
Hi jstreich,

The given if statement
if(index < 0 && index >= arrayLength)
will never become true (the arrayLength will not be <= 0).

Check it.

Maniraja S
 
opps, OR not and... that is what I get for copy-paste. switch && with ||... Oh, and you'd also need to include standard exception library for mine for the out_of_range constructor to be called.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top