multiple operator [] problem

Discussion in 'C++' started by Oliver Michael Milz, Jul 2, 2003.

  1. Hi *,


    I have a class called r_Point with two operators defined as follows:

    r_Range
    r_Point::eek:perator[]( r_Dimension i ) const throw( r_Eindex_violation )
    {
    return points;
    }

    r_Range&
    r_Point::eek:perator[]( r_Dimension i ) throw( r_Eindex_violation )
    {
    return points;
    }

    !!! This is not my hack, I just have to use it. !!!
    Can anyone please tell me what the hell the meaning of this is ?
    Under what circumstances is the first []operator called and under what
    the second ?
    There is twice the same implementation, differing only by the keyword
    const and the return type. In my eyes that should never compile.
    Actually it does...

    When I wanna use it:
    r_Point r_point =...;
    for(r_Dimension r=0; r<dimensioality; r++)
    {
    r_Range r_range = r_point[r];
    ....
    }
    the compiler won´t compile, cause it´s saying that r_point[r] is of type

    r_Point and not of r_Range.
    Like the operator [] would not have been overridden ...

    My fix was to create a method called getRange, whose implementation
    equals
    the one of the operators. That works. But is not really beautiful.


    Please help,
    Thanks,
    Oliver Milz
     
    Oliver Michael Milz, Jul 2, 2003
    #1
    1. Advertising

  2. "Oliver Michael Milz" <-muenchen.de> wrote...
    > Hi *,
    >
    >
    > I have a class called r_Point with two operators defined as follows:
    >
    > r_Range
    > r_Point::eek:perator[]( r_Dimension i ) const throw( r_Eindex_violation )
    > {
    > return points;
    > }
    >
    > r_Range&
    > r_Point::eek:perator[]( r_Dimension i ) throw( r_Eindex_violation )
    > {
    > return points;
    > }
    >
    > !!! This is not my hack, I just have to use it. !!!
    > Can anyone please tell me what the hell the meaning of this is ?


    What do you mean? Two functions overloaded on the types of arguments.
    This is a very common way to overload operators.

    > Under what circumstances is the first []operator called and under what
    > the second ?


    The first is called for constant objects, the second for non-constant.

    > There is twice the same implementation, differing only by the keyword
    > const and the return type. In my eyes that should never compile.


    You may need new eyes.

    > Actually it does...


    Of course it does, it's legal C++.

    >
    > When I wanna use it:
    > r_Point r_point =...;
    > for(r_Dimension r=0; r<dimensioality; r++)
    > {
    > r_Range r_range = r_point[r];
    > ....
    > }
    > the compiler won´t compile, cause it´s saying that r_point[r] is of type
    >
    > r_Point and not of r_Range.


    Post complete code. The fragments you posted do look convincing.
    However, if in fact your 'r_point' happens to be of type r_Point*
    (and you just forgot to mention that asterisk in your post), the
    compiler would say exactly what it says.

    > Like the operator [] would not have been overridden ...
    >
    > My fix was to create a method called getRange, whose implementation
    > equals
    > the one of the operators. That works. But is not really beautiful.


    I can believe that.

    Victor
     
    Victor Bazarov, Jul 2, 2003
    #2
    1. Advertising

  3. At first: thank you Victor.


    Victor Bazarov wrote:

    > "Oliver Michael Milz" <-muenchen.de> wrote...
    > > Hi *,
    > >
    > >
    > > I have a class called r_Point with two operators defined as follows:
    > >
    > > r_Range
    > > r_Point::eek:perator[]( r_Dimension i ) const throw( r_Eindex_violation )
    > > {
    > > return points;
    > > }
    > >
    > > r_Range&
    > > r_Point::eek:perator[]( r_Dimension i ) throw( r_Eindex_violation )
    > > {
    > > return points;
    > > }
    > > Under what circumstances is the first []operator called and under what
    > > the second ?

    >
    > The first is called for constant objects, the second for non-constant.


    Okay learned something.
    My excuse is : Have not seen something like that before.
    I just started programming c++ again while programming java for years.


    >
    > > There is twice the same implementation, differing only by the keyword
    > > const and the return type. In my eyes that should never compile.

    >
    > You may need new eyes.
    >


    that was a good one ;)

    >
    > >
    > > When I wanna use it:
    > > r_Point r_point =...;
    > > for(r_Dimension r=0; r<dimensioality; r++)
    > > {
    > > r_Range r_range = r_point[r];
    > > ....
    > > }
    > > the compiler won´t compile, cause it´s saying that r_point[r] is of type
    > >
    > > r_Point and not of r_Range.

    >
    > Post complete code. The fragments you posted do look convincing.
    > However, if in fact your 'r_point' happens to be of type r_Point*
    > (and you just forgot to mention that asterisk in your post), the
    > compiler would say exactly what it says.


    I will first retry it on my own.
    Guess you could be right.


    Thanks for your time,
    Bye Oliver
     
    Oliver Michael Milz, Jul 2, 2003
    #3
  4. Oliver Michael Milz

    Rolf Magnus Guest

    Oliver Michael Milz wrote:

    >> > Under what circumstances is the first []operator called and under
    >> > what the second ?

    >>
    >> The first is called for constant objects, the second for
    >> non-constant.

    >
    > Okay learned something.
    > My excuse is : Have not seen something like that before.
    > I just started programming c++ again while programming java for years.


    Don't try to apply Java knowledge in C++ programming. It won't work.
    Also, you should read up on const correct programming.
     
    Rolf Magnus, Jul 2, 2003
    #4
    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. Jakob Bieling

    Q: operator void* or operator bool?

    Jakob Bieling, Mar 5, 2004, in forum: C++
    Replies:
    2
    Views:
    587
    Rob Williscroft
    Mar 5, 2004
  2. John Smith
    Replies:
    2
    Views:
    423
    Ivan Vecerina
    Oct 6, 2004
  3. Afshin
    Replies:
    0
    Views:
    294
    Afshin
    Aug 12, 2011
  4. Afshin
    Replies:
    0
    Views:
    300
    Afshin
    Aug 12, 2011
  5. Afshin
    Replies:
    7
    Views:
    440
    Victor Bazarov
    Aug 16, 2011
Loading...

Share This Page