C++ square bracket overloading

Ixonal

New Member
Messages
29
Reaction score
0
Points
0
is it possible to overload square brackets on an object to take a string input or is it integer only?
 

Ixonal

New Member
Messages
29
Reaction score
0
Points
0
just read some wiki that said it's possible, but didn't give an example. when I try to do it, I get this...

Code:
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

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 work
 
Last edited:

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
The square bracket operator is an unary operator, and thus has the following signature.

Code:
return_type operator[] (index_type);

It's covered by section 13.5.5 [over.sub] of the C++ standard, which allows for arbitrary index type.
 
Top