1

I have to combine two programs that access a matrix with double [][] and single [] array index operator in their respective code.

Instead of having two matrix types, and converting between them, the solution is to overload the single matrix class with a single and double array index operator

My class currently looks like this:

struct Matrix4x4
{
    float mElements[16];

    class Proxy {
    public:
        Proxy(float* _array, int index_1) : _array(_array), index_1(index_1) { }

        int operator[](int index_2) {
            return _array[index_1 * index_2];
        }
    private:
        float* _array;
        int index_1;
    };

    Proxy operator[](int index_1) {
        return Proxy(mElements, index_1);
    }

    float TestDoubleIndexAccess()
    {
        return this[0][0];
    }
}

In the function TestDoubleIndexAccess

I get the error No suitable conversion function from Matrix4x4::Proxy to float exists

My understanding is the second set of brackets is supposed to call operator[] of Matrix4x4::Proxy return. But it is not.

Next, I am currently accessing the single matrix using

Matrix4x4Instance.mElements[i]

I would like to change this to Matrix4x4Instance[i], but I cannot overload the same operator twice

Is is possible to create a matrix class with overloaded double and single array indexing operator? How can it be done elegantly, using only bracket operators and not function calls?

Mich
  • 3,188
  • 4
  • 37
  • 85

0 Answers0