Noah Roberts wrote:
Yes. The concept of forcing [][] on an object because it is
implemented with an array is inherently flawed. Try implementing [][]
with a bitboard for example and then compare that implementation to
functional notation.
class Board
{
private:
uint64 white_pieces;
uint64 black_pieces;
uint64 white_knights;
....
? operator[](uint row);
// easier and more appropriate:
piece pieceAt(uint row, uint col);
piece operator()(uint row, uint col);
};
Now, in actually building a chess engine you're probably going to break
encapsulation anyway and create massive dependencies just to speed
things up a hair (if you've never done it let me tell you that a LOT of
time is spent trying to get just a little more oomph out of your
classes) but at any rate this is a perfect example of what happens when
you think, "Hey this is an array, lets make it look like one..." Well,
what happens when you decide it shouldn't be implemented with an array
anymore??
I must say I cannot see your point. The rational of using operator
overloading is not whether the internals of the class are with array or
something else. It is intending to make it look like array.
The point is that in imposing the array semantics on the above board
object you have created a depenency on the internals. It would be
difficult and inefficient to implement that interface with a
bitboard...if you doubt that then try it.
Anyone
using C++ is accustom to using [] for accessing continues memory.
Using objects is about abstraction.
This
is why you have it in std::vector and std::string but you don't at
std::list.
Conceptually both vector and string are arrays. That is the
abstraction they are. Board, Matrix, etc...are all abstractions above
that. You can argue that a 2d array in fact is conceptually an array
and this would be correct but there are two things to consider. First,
implementing that correctly is not as simple as creating some storage
medium and then exposing it to the public. You need abstractions or
you are better off with the more natural primative type itself. The
second thing to consider is that you already have 1d arrays that can,
used together, do everything you might need of a 2, 3, 4, etc dim
array. In an effort to use the simplest method to get the job you need
done why reinvent the wheel for each dimension? Why not use the tools
at your disposal that do the job you need done?
At any rate...Board and Matrix as types or abstractions are NOT arrays.
I gave a perfect example of why pretending they are is a problem...A
board and matrix both have two dimensional coordinates and have an
"area" but they are an abstraction above array and thus shouldn't have
array semantics imposed upon them - they could use an array inside but
shouldn't expose that to the outside in any way, including especially
their own interface. I would argue that the same is true for the OP's
bitmap class.