user3352874
user3352874

Reputation: 15

Custom index operator C++

I am having trouble building a class that makes sure the user does not access an element that is off the end of the array, by building a class that mimics the behavior of an array, but adds a check. That is, this class will create a sequence of elements of a given type, and allow access to these elements with the [] bracket operator, but it will check to make sure that the user does not try to do something with an element that doesn't exist.

Here is the instructions on building it.

I have no idea how to make an index operator for this case. Please help me. Thanks!

Here is my 3 files I have so far...

dvd.h

class dvdArray{

    dvd *elt;
    int size;
    static int defaultSieze;
    int getSize();
    void display();




    dvdArray(unsigned int sz);
    dvdArray();
    dvdArray(dvdArray &obj);
    ~dvdArray();

}; 

dvd.cpp

dvd::dvd(){
    id =0;
    int n=5;

    title = new char [n];
    director = new char [n];



    title[0] = '\0';

    director[0] = '\0';

}
dvd::~dvd(void)
{

}
dvdArray::dvdArray(unsigned int sz){
    elt = new dvd[sz];
}
dvdArray::dvdArray(){
    size = defaultSieze;
    elt = new dvd[defaultSieze];

}
dvdArray::dvdArray(dvdArray &obj){

    size = obj.size;
    elt = new dvd[defaultSieze];
    for (int i=0; i!='\0'; ++i) {
        elt[i]=obj.elt[i];

        }

}
dvdArray::~dvdArray(void)
{

}

Upvotes: 0

Views: 2660

Answers (2)

Frustrated Soul
Frustrated Soul

Reputation: 361

I would do something simple.. General purpose array, bounds checked... Not sure why you would need that however... Vectors are a very good alternative to arrays.

template <typename T> class myArray{
    size_t size;
    T *arr;
    void allocate(size_t s){
        if(s<1) arr = NULL;
        else arr = new T[s];
        size = s;
    }
public:
    myArray(){ allocate(10); } //default constructor
    myArray(size_t s){ allocate(s); }
    ~myArray(){ if(arr!=NULL) delete[] arr; arr=NULL; }
    T& operator[] (size_t s) {
        if(s>=size) throw Error(); //or do whatever 
        else return arr[s];
    }
};

Upvotes: 0

Tony Delroy
Tony Delroy

Reputation: 106086

The easiest / cleanest thing to do is to derive from std::vector (or std::array if it suits your purposes better), which is safe as long as you don't then delete the object using a std::vector*, and as long as the functions you want to do checked access accept the parameter as a checked_vector and not a std::vector*/&...

Example:

template <typename T>
class checked_vector : public std::vector<T>
{
  public:
    // ...forwarding constructors
    using std::vector::vector;

    T& operator[](size_t n) { return at(n); }
    const T& operator[](size_t n) const { return at(n); }
};

Note: that won't protect you from invalid use of iterators, such as incrementing them too far or adding an illegal offset to them.

If - for whatever reason - you're determined to use your own implementation...

dvd& dvdArray::operator[](size_t n)
{
    if (n >= size)
        throw std::runtime_error("invalid array index");
    return dvd[sz];
}

const dvd& dvdArray::operator[](size_t n) const
{
    if (n >= size)
        throw std::runtime_error("invalid array index");
    return dvd[sz];
}

Upvotes: 1

Related Questions