Throwing an exception when an array goes out of bounds

I have a matrix class that uses [] [] to access elements. When one (or both) indexes are out of bounds, I should throw a CIndexException. This is a class that stores text in this format "Invalid index [a] [b]", where a and b are numbers.

This is my current implementation of the CIndexException class

class CIndexException
{
    string text;
public:
    CIndexException (int a, int b)
    {
        ostringstream oss;
        oss << "Invalid index [";
        oss << a;
        oss << "][";
        oss << b;
        oss < "]";

        text = oss.str();
     }

     string get() const
     {
         return text;
     }
};

The matrix is ​​represented as a two-dimensional array of twins, it is initialized in the constructor:

CMatrix(int r, int c)
{
    colls = c;
    rows = r;
    mat = new double * [rows];

    for (int i = 0; i < rows; i++)
    {
        mat[i] = new double [colls];
        for (int j = 0; j < colls; j++)
            mat[i][j] = 0;
    }
}

To get one item, I overloaded this statement as follows:

double * operator[] (int x) const
{
    return mat[x];
}

When I type a [2] [3], this function resolves the first [], returns a pointer to the array, and the second [] resolves, as usual.

, , . MatrixRow, . MatrixRows. [] [] , []. , , , "" . ?

+5
2

MatrixRow, MatrixRow . MatrixRow , .

+3

operator()() operator[](). . . , , .

+1

All Articles