index operator overload

Discussion in 'C++' started by Philipp Kraus, Sep 17, 2011.

  1. Hello,

    I would like to overload in my class the [] or () operator. Many
    tutorials create the call like
    myvaluetype& operator[] (const int&)
    or
    myvaluetype operator[] (const int&)

    but is this correct? An index is in my opinion >= 0, so I should use a
    uint like std::size_t .
    The call can be also const, because it returns a value with no modify
    of object data.

    Is there a coding style guide or anything else for a correct call? I
    would like to create
    my overloading call to:
    myvaluetype operator[] (const std::size_t&) const;

    Thanks

    Phil
     
    Philipp Kraus, Sep 17, 2011
    #1
    1. Advertising

  2. Philipp Kraus

    Marc Guest

    Philipp Kraus wrote:

    > I would like to overload in my class the [] or () operator. Many
    > tutorials create the call like
    > myvaluetype& operator[] (const int&)


    Don't pass builtin types (like int) by reference but by value instead.

    > or
    > myvaluetype operator[] (const int&)


    This one should likely have a "const" at the end.

    > but is this correct? An index is in my opinion >= 0, so I should use a
    > uint like std::size_t .


    It is a matter of taste that gives rise to a lot of trolling from both
    sides.

    > The call can be also const, because it returns a value with no modify
    > of object data.


    Note that the first version you wrote above returns a non-const
    reference, so it enables you to modify the object and can't be const.

    > Is there a coding style guide or anything else for a correct call? I
    > would like to create
    > my overloading call to:
    > myvaluetype operator[] (const std::size_t&) const;


    * pass size_t by value
    * overload sort of implies there are 2 versions. Where is the other
    one?
     
    Marc, Sep 17, 2011
    #2
    1. Advertising

  3. Philipp Kraus

    Noah Roberts Guest

    On Sep 17, 9:18 am, Marc <> wrote:
    > Philipp Kraus  wrote:


    > > Is there a coding style guide or anything else for a correct call? I
    > > would like to create
    > > my overloading call to:
    > > myvaluetype operator[] (const std::size_t&) const;

    >
    > * pass size_t by value
    > * overload sort of implies there are 2 versions. Where is the other
    >   one?


    When you're talking about "operator overloading" the "other one" is
    the standard operator.
     
    Noah Roberts, Sep 17, 2011
    #3
  4. Philipp Kraus

    Marc Guest

    Noah Roberts wrote:

    > When you're talking about "operator overloading" the "other one" is
    > the standard operator.


    Ah, yes, it makes sense, thanks.
     
    Marc, Sep 17, 2011
    #4
  5. Philipp Kraus

    Nobody Guest

    On Sat, 17 Sep 2011 17:23:25 +0200, Philipp Kraus wrote:

    > I would like to overload in my class the [] or () operator. Many tutorials
    > create the call like
    > myvaluetype& operator[] (const int&)
    > or
    > myvaluetype operator[] (const int&)
    >
    > but is this correct? An index is in my opinion >= 0, so I should use a
    > uint like std::size_t .


    The STL containers which provide operator[] use an unsigned type.

    > The call can be also const, because it returns a value with no modify of
    > object data.


    The first one above returns a non-const reference, so it can't be const.

    > Is there a coding style guide or anything else for a correct call? I would
    > like to create
    > my overloading call to:
    > myvaluetype operator[] (const std::size_t&) const;


    If the objects actually exist within the container (rather than being
    generated on the fly), you should normally return a reference rather than
    a copy.
     
    Nobody, Sep 17, 2011
    #5
  6. Philipp Kraus

    James Kanze Guest

    On Sep 17, 4:23 pm, Philipp Kraus <> wrote:

    > I would like to overload in my class the [] or () operator.
    > Many tutorials create the call like
    > myvaluetype& operator[] (const int&)
    > or
    > myvaluetype operator[] (const int&)


    > but is this correct?


    It depends. The latter should almost certainly have a 'const'
    at the end of it, and it's quite exceptional to pass int by
    reference.

    > An index is in my opinion >= 0, so I should use a
    > uint like std::size_t .


    First, there's nothing which says an index can't be negative;
    I've written classes where it was. And secondly, you have to
    consider the global context: if the indexes are numeric, it
    makes sense to take the difference between two indexes, using
    the formula 'abs(index1 - index2)'; if this expression doesn't
    work, the index doesn't have an appropriate type.

    Ideally, of course, C++ would have subrange types, which could
    only hold the value [lower_bound...upper_bound) (but whose
    subtraction would result in the correct value, probably of type
    int). C++ doesn't, however, so we make do with int.

    > The call can be also const, because it returns a value with no modify
    > of object data.


    > Is there a coding style guide or anything else for a correct call?


    The practices in "Programming: Principles and Practice using
    C++" would be a good starting point.

    --
    James Kanze
     
    James Kanze, Sep 19, 2011
    #6
  7. Thanks a lot to all for your ansers

    Phil
     
    Philipp Kraus, Sep 20, 2011
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mr Smith
    Replies:
    7
    Views:
    489
    Mr Smith
    Feb 10, 2005
  2. Piotre Ugrumov
    Replies:
    3
    Views:
    397
    Nick Hounsome
    Jan 25, 2004
  3. xkenneth
    Replies:
    7
    Views:
    450
  4. Ying-Chieh Liao

    function overload (not operator overload)

    Ying-Chieh Liao, Oct 11, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    281
    Sherm Pendley
    Oct 11, 2004
  5. Tomasz Chmielewski

    sorting index-15, index-9, index-110 "the human way"?

    Tomasz Chmielewski, Mar 4, 2008, in forum: Perl Misc
    Replies:
    4
    Views:
    358
    Tomasz Chmielewski
    Mar 4, 2008
Loading...

Share This Page