m4ster r0shi wrote: |
---|
This is good for two reasons:
[snip]
(2) That syntax is the common 2D array syntax. |
That's kind of my thing -- I dont' see that as a good thing. Personally I find [y][x] syntax to generally be harder to follow. But besides that....
Downsides include:
1) double indirection (ie slower) (multidimentional via 2 vectors = 2x the indirection)
2) more memory consumed than necessary (ie bulkier) (again due to 2 vectors)
3) inconsistent types returned from [] operator.
(also, throwing '911' is kind of silly -- it'd be better to throw a range error:
http://cplusplus.com/reference/std/stdexcept/range_error/ -- but whatever. I figured you're doing that just because he did in his original example?)
#1 and 2 can be avoided with this approach if you do the [y*width+x] lookup, but doing that via double brackets and a Row class is even more clutter and unnecessarily complicated code. On the other hand, 2x vectors does make natural resizing quite a bit easier (not that it's impossible the other way -- but it's certainly more work)
#3 isn't as big of a deal since Row is private -- but that pretty much means the
only way you can use this class is with the double brackets (using single brakets isn't an option).
So what's the point of double brakets? Isn't the benefit of using them the option that you can only use one if you want?
You're also cluttering the entire class with #ifdefs since the class is defined totally differently for both builds. It'd probably be better to just write two different classes and #ifdef the whole thing rather than futily trying to #ifdef only bits and pieces. But of course that means double maintanance, somewhat duplicate code.
It's just ugly on so many levels. And for what?
I'm tempted to write my own class for contrast, but it's so late.
EDIT: "doing it right" with placement new and shifting everything around when you resize would actually be a considerable amount of work. You wouldn't really be able to use vector for that.
I could even implement [y][x] (without bounds checking) as well as (x,y) -- but I don't think that'd be a good idea as [y][x] would have more overhead and lacks bounds checking, and leads to bad habits.