Skip to main content
gamedev.net
🔒 Locked

Overloading [][] operator

Started by Eyrian 6 replies 900+ views
Original Post
Eyrian
Eyrian
Is there any way to overload the [][] operator? Is it even an operator? What I am doing is creating a sort of matrix wrapper class (there''s some other data in there, too), and I wanna retain the ability to access the elements of the matrix directly, using the double subscripts. Can I do this, or will I need to find some tricky way to use only single subscripts? Would inheritance be a good solution? Ciao, Xeth
mictian
mictian
overload the () operator instead.

i.e.

int operator() ( int x, int y ) {

return( m_matrix[x][y] );

}

or somehting

¿asd?

[edited by - mictian on April 25, 2003 8:52:14 PM]
¿We Create World?
abembenek
abembenek
I''m not sure, but maybe you can try overloading the bracket operator with more than one parameter, like this:

operator[](int idx1, ...)

Just a guess. Good luck.
Cedric
Cedric
You can''t; it''s an unary operator. As has been suggested, you should overload the () operator instead.

The other solution is to overload [] and have it return an object of another type, for which operator [] is also overloaded. For example, a matrix could return a vector.

Cédric
chiuyan
chiuyan
you can fake implement it by having the [] operator of one class return something that has a [] operator. something like


class MatrixLine
{
public:
MatrixLine()
{
for (int iFloat = 0; iFloat < 10; iFloat ++)
{
m_aFloats[iFloat] = float(iFloat);
}
}
float operator[](const int iIndex)
{
return m_aFloats[iIndex];
}
private:
float m_aFloats[10];
};

class Matrix
{
public:

MatrixLine& operator[](const int iIndex)
{
return m_aMatrixLines[iIndex];
}
private:
MatrixLine m_aMatrixLines[10];
};

int main(int argc, char* argv[])
{
Matrix M;
const float rVal = M[0][1];

return 0;
}


I have never done something like this, and can't say that i would recommend it, but you could :-)

(Edit for formatting)


[edited by - chiuyan on April 25, 2003 9:20:15 PM]
Shannon Barber
Shannon Barber
float* operator[](int i)
{
return &m_aFloats[ i ];
}

You can return a proxy-object that overloads operator[] to do fancy stuff.

Note that overloading operator() is typical, since you don't have as much confusion over the row-column majoring as you do with the [][] case. The above code could easily be wrong and could easily stomp over the wrong memory if used incorrectly.

[edited by - Magmai Kai Holmlor on April 25, 2003 10:46:47 PM]
The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Sneftel
Sneftel
from the C++ FAQ-lite:

quote:

Some people build a Matrix class that has an operator[] that returns a reference to an Array object, and that Array object has an operator[] that returns an element of the Matrix (e.g., a reference to a double). Thus they access elements of the matrix using syntax like m[ i][j] rather than syntax like m(i,j).

The array-of-array solution obviously works, but it is less flexible than the operator() approach. Specifically, there are easy performance tuning tricks that can be done with the operator() approach that are more difficult in the [][] approach, and therefore the [][] approach is more likely to lead to bad performance, at least in some cases.

For example, the easiest way to implement the [][] approach is to use a physical layout of the matrix as a dense matrix that is stored in row-major form (or is it column-major; I can't ever remember). In contrast, the operator() approach totally hides the physical layout of the matrix, and that can lead to better performance in some cases.

Put it this way: the operator() approach is never worse than, and sometimes better than, the [][] approach.

  • The operator() approach is never worse because it is easy to implement the dense, row-major physical layout using the operator() approach, so when that configuration happens to be the optimal layout from a performance standpoint, the operator() approach is just as easy as the [][] approach (perhaps the operator() approach is a tiny bit easier, but I won't quibble over minor nits).

  • The operator() approach is sometimes better because whenever the optimal layout for a given application happens to be something otherthan dense, row-major, the implementation is often significantly easier using the operator() approach compared to the [][] approach.


As an example of when a physical layout makes a significant difference, a recent project happened to access the matrix elements in columns (that is, the algorithm accesses all the elements in one column, then the elements in another, etc.), and if the physical layout is row-major, the accesses can "stride the cache". For example, if the rows happen to be almost as big as the processor's cache size, the machine can end up with a "cache miss" for almost every element access. In this particular project, we got a 20% improvement in performance by changing the mapping from the logical layout (row,column) to the physical layout (column,row).

Of course there are many examples of this sort of thing from numerical methods, and sparse matrices are a whole other dimension on this issue. Since it is, in general, easier to implement a sparse matrix or swap row/column ordering using the operator() approach, the operator() approach loses nothing and may gain something - it has no down-side and a potential up-side.



How appropriate. You fight like a cow.

[edited by - sneftel on April 25, 2003 10:49:37 PM]

[edited by - sneftel on April 25, 2003 10:50:16 PM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.