Implementing [][] operator

Started by
14 comments, last by Shakedown 15 years ago
Quote:Original post by gekko
You can provide error checking on the row, but not the column indexing. This approach is ... will get the job done.

Those two statements contradict each other.
Advertisement
No they don't. Error checking may be recommended, but isn't necessary. As already mentioned, this needs to work with already existing code, which we know nothing about. The original poster may know that all the values being passed in are valid, and instead wants higher performance. The case has been made in favor of error checking, but there are other options.
-- gekko
Quote:Original post by Captain_Thunder
Quote:Original post by Shakedown
I understand the performance costs, but I've got to make my class work with existing code.


Since no one seems to be addressing this - what exactly is your existing code doing that forces you to use [][] instead of ()?


Haha, the existing code is doing this:

their2dArray[][]

And I need:

my2dArray[][]
Quote:Original post by gekko
If you want to implement [], I recommend storing your matrices in row-major order...


If I'm going to use an inner class to represent a Row, does this approach still work?

In that case, I'd overload the [] operator in 2 classes:
- My2DArray class, and
- My2DArray::Row class

As it's been said before in the thread, yes, that's the way to do it.
Watch out for object scopes when using references.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Quote:Original post by Fruny
As it's been said before in the thread, yes, that's the way to do it.
Watch out for object scopes when using references.


Ah, I meant was it still possible to do row-major ordering when using an inner class for the row. I assume so...

Maybe I should stop asking questions and start implementing it!

This topic is closed to new replies.

Advertisement