+ Reply to Thread
Results 1 to 4 of 4

Thread: C++ square bracket overloading

  1. #1
    Ixonal is offline x10Hosting Member Ixonal is an unknown quantity at this point
    Join Date
    Sep 2008
    Posts
    29

    C++ square bracket overloading

    is it possible to overload square brackets on an object to take a string input or is it integer only?

  2. #2
    Tariqul Islam's Avatar
    Tariqul Islam is offline x10 Sophmore Tariqul Islam is an unknown quantity at this point
    Join Date
    Oct 2007
    Location
    Bangladesh
    Posts
    182

    Re: C++ square bracket overloading

    I am also interested to know the answer.



  3. #3
    Ixonal is offline x10Hosting Member Ixonal is an unknown quantity at this point
    Join Date
    Sep 2008
    Posts
    29

    Re: C++ square bracket overloading

    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 by Ixonal; 09-16-2009 at 09:17 AM.

  4. #4
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: C++ square bracket overloading

    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.
    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.

+ Reply to Thread

Similar Threads

  1. finding square root in php for 30 credits
    By nahsorhseda in forum Programming Help
    Replies: 11
    Last Post: 03-24-2008, 09:03 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers