is it possible to overload square brackets on an object to take a string input or is it integer only?
is it possible to overload square brackets on an object to take a string input or is it integer only?
just read some wiki that said it's possible, but didn't give an example. when I try to do it, I get this...
edit: gah, I'm an idiot, it works. I had mytable["PI"], but mylist is a pointer, so it needed to be (*mytable)["PI"] to workCode:ix_hash_test.cpp: In function ‘int main(int, char**)’: ix_hash_test.cpp:21: error: invalid types ‘ixHash<int>*[const char [3]]’ for array subscript
Last edited by Ixonal; 09-16-2009 at 09:17 AM.
The square bracket operator is an unary operator, and thus has the following signature.
It's covered by section 13.5.5 [over.sub] of the C++ standard, which allows for arbitrary index type.Code:return_type operator[] (index_type);
Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.Misson, not Mission.