Re: Can we override [][] ?
Noah Roberts wrote:
Axter wrote:
Noah Roberts wrote:
Jim Langston wrote:
I wanted to do an operator override for [][] but couldnt' figure out the
syntax.
http://www.parashift.com/c++-faq-lite/operator-overloading.html#faq-13.10
I recommend NOT following this particular FAQ.
It uses contrive logic to support promoting the use of non-standard
syntax to reference a matrix.
"Non-standard"? Non-standard to who? Certainly not the domain which
uses subscript notation and/or M(a,b)...or the unimplmentable M[a,b].
I recommend using standard syntax [][] over more ambiguous ()() method.
"ambiguous"? What is so special about [][] that makes it less
ambiguous than ()?
I also recommmend using vector<vector<T> > type.
Can't enforce size.
Requires dynamic allocation.
Etc...etc...
See following links for example implementations:
http://code.axter.com/dynamic_2d_array.h
Above example is horrid still. Again it may track its own size but it
neither provides bounds checking nor does it provide a client interface
to retrieve bounds so there is frankly no reason for it to track its
size at all (only m_col is used and neither are accessable). The fact
that it contains redudant check for null of a pointer wouldn't be so
bad if it wasn't being passed off as an example and didn't have so many
other problems.
The purpose of the dynamic_2d_array, is to give a dynamic version of a
static size C-style array.
It's no more or no less safer then using a static C-Style array, and
therefore, no more horrid then using a static size C-Style array.
If you need something that requires bounds checking, then you should
not use it.
The class is not intended for that purpose.
http://www.codeguru.com/forum/showthread.php?t=231046
Also very incomplete
These are example skeleton classes. They don't have to be complete.
They just need enough to get the point across as to how to get a [][]
interface.
Also a rather inefficient implementation due to the reasons you
describe in the first...I agree that a single, contiguous array is much
more efficient.
I heard this from most programmers, who don't have the experience in
actually testing this theory out.
I've conducted test, shows your above comment to be false.
Try testing this in RELEASE version, and you'll find very little
difference in performance.
In fact, some implementations have better performance with a
vector<vector< > >, then with a contiguous array.
I suggest that if you are going to argue with this newsgroup's faqs you
should at least provide reasonable implementations of what you deem the
correct way.
I suggest, that you first test your theories before making performance
claims on this newsgroup.
The implementations I posted as examples, are reasonable for their
purpose.
The ones you have provided are dangerous, incomplete, and
useless in the real world.
And again, they're no more dangerous then using static size C-Style
arrays.
It's incorrect to believe that all code requires bounds checking.
That's why the C++ standard doesn't require that STL have bounds
checking.
If' it's good for the standard, then it's good for a skeleton example
implementation.
Following an FAQ blindly, is a mistake.
This C++ FAQ is created by one person, and he is no more or no less
prone to mistakes then any other experience programmer.
IMHO, this FAQ is wrong, and I posted alternative skeleton examples
that can be used as a template for user's custom matrix class.
IMHO, your nonconstructive comments do a disservice to the C++
community and to this newsgroup.