I am creating a matrix math library which I would like to give the user access an element in the NxM 2D array, that was allocated in the constructor. I've been reading that you can't overload the [][]. How would I be able to overload this overload this operator so that the user can access an element in a two dimensional space.

Evan

Ok thanks that helps. I didn't know [][] could be a performance hit over (,)

About this thread title.
Nobody can overload [][] because [][] is NOT an operator at all. This construct designates TWO [] binary operators in succession. It's a senseless construct in C++.

Try overload operator().

This Fortran-like form of 2D array element accessor via overloaded operator(int,int) does not fit in C++ regular 2D array access style. I think it's not the best idea to introduce different accessor syntax for class Matrix objects (as m(i,j) ) and regular 2D arrays (as m[i][j] . Probably it's confused and error-prone approach. Besides that we need to define yet another member to get pointers to matrix rows...

It's a moot point, of course...

Yea I agree. I'm going to work with (int, int) for right now but I would like to supply [][] down the road to the user if that's the from that they're familiar with.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.