0

just wrote a code for a template array class (I know its not finished yet), and trying to remember how to overload operators (meanwhile without special difficulties...).

Anyway, while thinking of how to implement operator[] I wondered what would happen if the index is outside the boundaries of the array... I'm pretty sure it is not possible for me to return a NULL (because of the return type), right? and if so, what should I return in case the index is out of boundries?

here's the code, most of it is redundant to my question, but it might help anyone who google's operators overloading so I post the complete code...

#ifndef __MYARRAY_H
#define __MYARRAY_H

#include <iostream>
using namespace std;

template <class T>
class MyArray
{
    int phisicalSize, logicalSize;
    char printDelimiter;
    T* arr;

public: 
    MyArray(int size=10, char printDelimiter=' ');
    MyArray(const MyArray& other);
    ~MyArray() {delete []arr;}

    const MyArray& operator=(const MyArray& other);
    const MyArray& operator+=(const T& newVal);

    T& operator[](int index);

    friend ostream& operator<<(ostream& os, const MyArray& ma)
    {
        for(int i=0; i<ma.logicalSize; i++)
            os << ma.arr[i] << ma.printDelimiter;
        return os;
    }
};

template <class T>
T& MyArray<T>::operator[]( int index )
{
    if (index < 0 || index > logicalSize)
    {
        //do what???
    }

    return arr[index];
}

template <class T>
const MyArray<T>& MyArray<T>::operator+=( const T& newVal )
{
    if (logicalSize < phisicalSize)
    {
        arr[logicalSize] = newVal;
        logicalSize++;
    }

    return *this;
}

template <class T>
const MyArray<T>& MyArray<T>::operator=( const MyArray<T>& other )
{
    if (this != &other)
    {
        delete []arr;
        phisicalSize = other.phisicalSize;
        logicalSize = other.logicalSize;
        printDelimiter = other.printDelimiter;
        arr = new T[phisicalSize];

        for(int i=0; i<logicalSize; i++)
            arr[i] = other.arr[i];
    }

    return *this;
}

template <class T>
MyArray<T>::MyArray( const MyArray& other ) : arr(NULL)
{
    *this = other;
}

template <class T>
MyArray<T>::MyArray( int size, char printDelimiter ) : phisicalSize(size), logicalSize(0), printDelimiter(printDelimiter)
{
    arr = new T[phisicalSize];
}

#endif
omi
  • 145
  • 1
  • 2
  • 14
  • 1
    You should -at most- _assert_, its a bug to index an array beyond its bounds... – K-ballo Jan 10 '13 at 01:34
  • 4
    `__MYARRAY_H` is a [reserved](http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier) identifier. – chris Jan 10 '13 at 01:34
  • 2
    You could throw an exception. – brain Jan 10 '13 at 01:36
  • 1
    You could return first and last element, essentially clamping the index to the array size, after the assert ofcourse. – Karthik T Jan 10 '13 at 01:36
  • @chris thanks, didn't know that! anyway it doesn't effect my debuging, but for next time I'll know better :) – omi Jan 10 '13 at 01:39
  • @K-ballo I thought I know c++ very well, and every time I post a question here, I realize how little I know- _assert_ is a brand new consept for me... thanks! – omi Jan 10 '13 at 01:57

1 Answers1

6

operator[] generally does no bounds checking. Most of the standard containers that can have a range utilize a separate function, at(), which is range checked and throws a std::out_of_range exception.

You likely want to implement a const T& operator[] overload, too.

Yuushi
  • 25,132
  • 7
  • 63
  • 81
  • just now iplemented 'const T& operator[]' :) about the exception- im not sure i want to use exceptions... although its a good solution. – omi Jan 10 '13 at 01:43