There are a few ways to do this, depending on the behavior you want. You can make a class that contains the array (constructor will take the size of the array). Then you implement/overload all the operators you are interested in. In these operators, you can overload [] to cause the index to wrap (if the size of the array was 10 and you did [10] you would get the 0'th element. Or you could have [10] return [9] element all the time. With templates:
template%26lt;class T, int n%26gt; class Array{
public:
T%26amp; operator[](int i) {
int j = i;
if(i %26lt; 0){
j = 0;
}
if(i %26gt; n - 1){
j = n - 1;
}
return data[j];}
private:
T data[n];
};
or something like that.
How do you implement an array class in c++ that solves the index out of bounds problem?
RTFM
Reply:search for RTFM at http://www.searchdub.com
Reply:The short answer, implement the operator[] operator. The long answer, get a good book on C++.
I think a better question is why does odysseus2i have the exact same avatar as you?
Reply:Chaeck in the sources class from MFC and you can find the sources of CArray.
it is very powerfull and reusable; just copy and paste and make little modification to use it in standard C++ (I used it in Borland C++ 3.1 for DOS)
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment