0

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

4
Contributors
6
Replies
7
Views
8 Years
Discussion Span
Last Post by Talguy
0

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

0

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++.

0

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...

0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.