Overloading []

Discussion in 'C++' started by Steve, Apr 20, 2004.

  1. Steve

    Steve Guest

    Hi,

    I have two classes: pair, and Array. In Array, I have overloaded the
    subcript operator like this:

    pair& operator[] (int index) {
    return pair(index);
    }

    However, it results in a compilation error saying that there's not
    suitable funtion that returns a 'pair' when I do:

    pair a = myArray[0];

    It seems to be always returning a reference of type Array. How can I
    make it return another type? Thanks,

    Steve
    Steve, Apr 20, 2004
    #1
    1. Advertising

  2. Steve

    Rakesh Guest

    Steve wrote:

    > Hi,
    >
    > I have two classes: pair, and Array. In Array, I have overloaded the
    > subcript operator like this:
    >
    > pair& operator[] (int index) {
    > return pair(index);
    > }


    >
    > It seems to be always returning a reference of type Array. How can I
    > make it return another type? Thanks,\


    Your function declaration ( and hence, the definition) says that .
    Try to look at the return type of the method - "pair& operator[] (int
    index) { .. } "

    --
    Rakesh Kumar
    ** Remove nospamplz from my email address for my real email **
    Rakesh, Apr 20, 2004
    #2
    1. Advertising

  3. Steve

    Steve Guest

    Steve wrote:
    > Hi,
    >
    > I have two classes: pair, and Array. In Array, I have overloaded the
    > subcript operator like this:
    >
    > pair& operator[] (int index) {
    > return pair(index);
    > }


    I know this is syntactically incorrect but I'm doing something more
    complicated than this and it does return a valid reference to a valid
    'pair' object, and not just a temporary as show above. However, my
    question is only this: how can I make it return another type?

    Steve
    Steve, Apr 20, 2004
    #3
  4. Steve

    Rakesh Kumar Guest

    The rule is -

    if you have function signature like this -
    "pair& operator[] (int )"

    pair & ref = MyObj[3]; // MyObj.operator[3]
    // Perfectly fine.

    pair ref = MyObj[3];
    // Wrong !! Since you are assigning a variable of type 'pair &'
    to 'pair'.

    HTH

    Steve wrote:
    >
    >
    > Steve wrote:
    >
    >> Hi,
    >>
    >> I have two classes: pair, and Array. In Array, I have overloaded the
    >> subcript operator like this:
    >>
    >> pair& operator[] (int index) {
    >> return pair(index);
    >> }

    >
    >
    > I know this is syntactically incorrect but I'm doing something more
    > complicated than this and it does return a valid reference to a valid
    > 'pair' object, and not just a temporary as show above. However, my
    > question is only this: how can I make it return another type?
    >
    > Steve
    >


    --

    --
    Rakesh Kumar
    ** Remove nospamplz from my email address for my real email **
    Rakesh Kumar, Apr 20, 2004
    #4
  5. Steve

    Steve Guest

    Rakesh Kumar wrote:
    > The rule is -
    >
    > if you have function signature like this -
    > "pair& operator[] (int )"
    >
    > pair & ref = MyObj[3]; // MyObj.operator[3]
    > // Perfectly fine.
    >
    > pair ref = MyObj[3];
    > // Wrong !! Since you are assigning a variable of type 'pair &'
    > to 'pair'.
    >
    > HTH


    But it should still not tell me that it can't initialize a value of type
    Array..:

    I still get this:
    app.cpp(121): error: a reference of type "pair &" (not const-qualified)
    cannot be initialized with a value of type "Array"
    pair &p = myArray[0];
    ^
    Steve, Apr 20, 2004
    #5
  6. Steve

    John Carson Guest

    "Steve" <nospam@nopes> wrote in message
    news:4084ab2c$
    > Rakesh Kumar wrote:
    >> The rule is -
    >>
    >> if you have function signature like this -
    >> "pair& operator[] (int )"
    >>
    >> pair & ref = MyObj[3]; // MyObj.operator[3]
    >> // Perfectly fine.
    >>
    >> pair ref = MyObj[3];
    >> // Wrong !! Since you are assigning a variable of type 'pair
    >> &' to 'pair'.



    Nonsense. It is perfectly permissible to assign a reference to pair to a
    pair.


    >
    > But it should still not tell me that it can't initialize a value of
    > type Array..:
    >
    > I still get this:
    > app.cpp(121): error: a reference of type "pair &" (not
    > const-qualified) cannot be initialized with a value of type "Array"
    > pair &p = myArray[0];
    > ^



    The following code works fine (it uses the standard pair, but this makes no
    difference --- my earlier version didn't). You need to show a compileable
    example of your code so we can see what is going wrong.


    #include <utility>
    using namespace std;

    class Array
    {
    pair<int,int> p[3];
    public:
    Array()
    {
    p[0].first = p[0].second = 10;
    p[1].first = p[1].second = 20;
    p[2].first = p[2].second = 30;
    }
    pair<int,int> & operator[](int index)
    {
    return p[index];
    }
    };

    int main ()
    {
    Array a;
    pair<int,int> p = a[1];
    p = a[0];
    return 0;
    }


    --
    John Carson
    1. To reply to email address, remove donald
    2. Don't reply to email address (post here instead)
    John Carson, Apr 20, 2004
    #6
  7. Steve

    Steve Guest

    Steve wrote:
    >
    >
    > Rakesh Kumar wrote:
    >
    >> The rule is -
    >>
    >> if you have function signature like this -
    >> "pair& operator[] (int )"
    >>
    >> pair & ref = MyObj[3]; // MyObj.operator[3]
    >> // Perfectly fine.
    >>
    >> pair ref = MyObj[3];
    >> // Wrong !! Since you are assigning a variable of type 'pair &'
    >> to 'pair'.
    >>
    >> HTH

    >
    >
    > But it should still not tell me that it can't initialize a value of type
    > Array..:
    >
    > I still get this:
    > app.cpp(121): error: a reference of type "pair &" (not const-qualified)
    > cannot be initialized with a value of type "Array"
    > pair &p = myArray[0];
    > ^
    >


    I know!! I'm also overloading the "=" operator and so it calls that
    before calling the [] operator! darn
    Steve, Apr 20, 2004
    #7
  8. Steve

    Jeff Schwab Guest

    Steve wrote:
    >
    >
    > Steve wrote:
    >
    >>
    >>
    >> Rakesh Kumar wrote:
    >>
    >>> The rule is -
    >>>
    >>> if you have function signature like this -
    >>> "pair& operator[] (int )"
    >>>
    >>> pair & ref = MyObj[3]; // MyObj.operator[3]
    >>> // Perfectly fine.
    >>>
    >>> pair ref = MyObj[3];
    >>> // Wrong !! Since you are assigning a variable of type 'pair
    >>> &' to 'pair'.
    >>>
    >>> HTH

    >>
    >>
    >>
    >> But it should still not tell me that it can't initialize a value of
    >> type Array..:
    >>
    >> I still get this:
    >> app.cpp(121): error: a reference of type "pair &" (not
    >> const-qualified) cannot be initialized with a value of type "Array"
    >> pair &p = myArray[0];
    >> ^
    >>

    >
    > I know!! I'm also overloading the "=" operator and so it calls that
    > before calling the [] operator! darn
    >


    No, it doesn't. I'll bet you a dollar to a doughnut you're returning a
    reference to a temporary.
    Jeff Schwab, Apr 20, 2004
    #8
  9. Steve

    Dave Moore Guest

    "John Carson" <> wrote in message news:<4084acd5$>...
    > "Steve" <nospam@nopes> wrote in message
    > news:4084ab2c$
    > > Rakesh Kumar wrote:
    > >> The rule is -
    > >>
    > >> if you have function signature like this -
    > >> "pair& operator[] (int )"
    > >>
    > >> pair & ref = MyObj[3]; // MyObj.operator[3]
    > >> // Perfectly fine.
    > >>
    > >> pair ref = MyObj[3];
    > >> // Wrong !! Since you are assigning a variable of type 'pair
    > >> &' to 'pair'.

    >
    >
    > Nonsense. It is perfectly permissible to assign a reference to pair to a
    > pair.
    >


    Well .. only if there is a copy constructor defined for class pair (
    at least for copy initialization, as specified in the code above and
    by the OP). Admittedly, it should be generated automatically, even if
    omitted from the class, but the OP *might* have done something like
    declare the copy constructor private for some reason. That would then
    generate a compiler error for the line in question, but I doubt it
    would be the message he actually received.
    Dave Moore, Apr 20, 2004
    #9
  10. Steve

    Dave Moore Guest

    Steve <nospam@nopes> wrote in message news:<4084ad04$>...
    > Steve wrote:
    > >
    > >
    > > Rakesh Kumar wrote:
    > >
    > >> The rule is -
    > >>
    > >> if you have function signature like this -
    > >> "pair& operator[] (int )"
    > >>
    > >> pair & ref = MyObj[3]; // MyObj.operator[3]
    > >> // Perfectly fine.
    > >>
    > >> pair ref = MyObj[3];
    > >> // Wrong !! Since you are assigning a variable of type 'pair &'
    > >> to 'pair'.
    > >>
    > >> HTH

    > >
    > >
    > > But it should still not tell me that it can't initialize a value of type
    > > Array..:
    > >
    > > I still get this:
    > > app.cpp(121): error: a reference of type "pair &" (not const-qualified)
    > > cannot be initialized with a value of type "Array"
    > > pair &p = myArray[0];
    > > ^
    > >

    >
    > I know!! I'm also overloading the "=" operator and so it calls that
    > before calling the [] operator! darn


    Sorry, but no ... operator[] has higher precedence than operator=. We
    can't diagnose your problem much further without some more code. You
    say you are overloading both the subscripting and assignment operators
    ... as shown in other examples in this thread, this can (and should)
    work just fine. Just post the relevant parts of the declarations of
    your pair and Array classes and someone will probably be able to spot
    the error.

    HTH, Dave Moore
    Dave Moore, Apr 20, 2004
    #10
  11. Steve

    Old Wolf Guest

    Steve <nospam@nopes> wrote:
    > > Rakesh Kumar wrote:
    > >> if you have function signature like this -
    > >> "pair& operator[] (int )"
    > >>
    > >> pair & ref = MyObj[3]; // MyObj.operator[3]
    > >> // Perfectly fine.

    > > I still get this:
    > > app.cpp(121): error: a reference of type "pair &" (not const-qualified)
    > > cannot be initialized with a value of type "Array"
    > > pair &p = myArray[0];
    > > ^


    The compiler is saying that the type of "myArray[0]" is "Array".
    You have not posted the declaration of "myArray", or the exact
    declaration of Array::eek:perator[].
    My money is on "myArray" being a pointer to Array (hence myArray[0]
    is an Array). Another possibility would be that it is an Array,
    and you had declared operator[] to return an Array.

    If you POST YOUR ACTUAL CODE then you will get an accurate answer.
    Scraps of stuff that is a little bit like your actual code, are next to
    useless.

    > I know!! I'm also overloading the "=" operator and so it calls that
    > before calling the [] operator! darn


    1) operator= is not called for the expression
    T p = expr;
    "expr" is converted to type T, using whatever conversions are
    defined, and then T's copy-constructor is called.
    2) If T is a reference type (in your example it is "pair &"), then
    operator= cannot be overloaded anyway.
    Old Wolf, Apr 20, 2004
    #11
  12. Steve

    ye hua Guest

    "Steve" <nospam@nopes> wrote in message
    news:4084ad04$...
    >
    >
    > Steve wrote:
    > >
    > >
    > > Rakesh Kumar wrote:
    > >
    > >> The rule is -
    > >>
    > >> if you have function signature like this -
    > >> "pair& operator[] (int )"
    > >>
    > >> pair & ref = MyObj[3]; // MyObj.operator[3]
    > >> // Perfectly fine.
    > >>
    > >> pair ref = MyObj[3];
    > >> // Wrong !! Since you are assigning a variable of type 'pair &'
    > >> to 'pair'.
    > >>
    > >> HTH

    > >
    > >
    > > But it should still not tell me that it can't initialize a value of type
    > > Array..:
    > >
    > > I still get this:
    > > app.cpp(121): error: a reference of type "pair &" (not const-qualified)
    > > cannot be initialized with a value of type "Array"
    > > pair &p = myArray[0];
    > > ^
    > >

    >
    > I know!! I'm also overloading the "=" operator and so it calls that
    > before calling the [] operator! darn
    >

    hi , what i want to say is that even your function return
    a pair , you can not use it correctly , because reference to
    a local variable is dangerous!
    ye hua, Apr 21, 2004
    #12
  13. Steve

    John Carson Guest

    "ye hua" <> wrote in message
    news:c64q70$m8e$-ag.de
    >
    > hi , what i want to say is that even your function return
    > a pair , you can not use it correctly , because reference to
    > a local variable is dangerous!


    See Steve's second post, where he says:

    "I know this is syntactically incorrect but I'm doing something more
    complicated than this and it does return a valid reference to a valid
    'pair' object, and not just a temporary as show above. However, my
    question is only this: how can I make it return another type?"


    --
    John Carson
    1. To reply to email address, remove donald
    2. Don't reply to email address (post here instead)
    John Carson, Apr 21, 2004
    #13
    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. Iyer, Prasad C

    Overloading __init__ & Function overloading

    Iyer, Prasad C, Sep 30, 2005, in forum: Python
    Replies:
    3
    Views:
    6,391
    Fredrik Lundh
    Sep 30, 2005
  2. Fredrik Lundh
    Replies:
    0
    Views:
    431
    Fredrik Lundh
    Sep 30, 2005
  3. Steve Holden
    Replies:
    0
    Views:
    418
    Steve Holden
    Sep 30, 2005
  4. Iyer, Prasad C
    Replies:
    4
    Views:
    563
    John J. Lee
    Sep 30, 2005
  5. Fredrik Lundh
    Replies:
    0
    Views:
    384
    Fredrik Lundh
    Sep 30, 2005
Loading...

Share This Page