Implementing [][] operator

Started by
14 comments, last by Shakedown 14 years, 11 months ago
I need to implement my2DArray[][] functionality in my class. I've found several articles online suggesting to use the () operator instead, but I want to figure this out using [][]. Since there is no [][] operator, there must be some 'trickery' or indirection happening behind the scenes to provide this indexing functionality. Here's what I've thought of so far: - Create an inner class representing a row in my 2d array class - Overload the [] operator to return an instance of this inner class - Overload the inner class' [] operator to return the correct element I don't see any obvious problems with this approach, although I'm not sure what I'd do if the user wanted access to an entire row (i.e. my2DArray[]) since I'd probably want to hide this inner class from the user. So, are there any other ways of achieving this functionality? Are there any problems or difficulties with my approach? Thanks
Advertisement
Quote:Original post by Shakedown
I need to implement my2DArray[][] functionality in my class.

clicky

Quote:Here's what I've thought of so far:

- Create an inner class representing a row in my 2d array class
- Overload the [] operator to return an instance of this inner class
- Overload the inner class' [] operator to return the correct element

I don't see any obvious problems with this approach

clicky

Quote:So, are there any other ways of achieving this functionality? Are there any problems or difficulties with my approach?

clicky
Yes I just read over that article before making my post! I understand what it said, but it didn't tell me how to implement [][] functionality, only not to do it.

I understand the performance costs, but I've got to make my class work with existing code.


I think you missed the third clicky.
Quote:Original post by Sneftel
I think you missed the third clicky.


No, I read that as well - hence my 'idea' for the inner class.

I guess my question is: Is that the best way to do it?
Quote:Original post by Shakedown
I guess my question is: Is that the best way to do it?

No. It's the only non-stupid way to do it.
Or you could use boost::multi_array, which already does this for you.
I find operator()(int x, int y) is a pretty good substitute.
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 ()?
If you want to implement [], I recommend storing your matrices in row-major order, ie. each row is together in memory:

00 01 02 03
04 05 06 07
08 09 10 11
12 13 14 15

You can easily do this with float[16]. Then overload operator [] to return a float* to whichever row you specified. Now using [] on a float* is automatic, and will handle indexing into the column. You can provide error checking on the row, but not the column indexing. This approach is fairly simple and will get the job done.
-- gekko

This topic is closed to new replies.

Advertisement