Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Overloading []

Reply
Thread Tools

Overloading []

 
 
Steve
Guest
Posts: n/a
 
      04-20-2004
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

 
Reply With Quote
 
 
 
 
Rakesh
Guest
Posts: n/a
 
      04-20-2004


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 **
 
Reply With Quote
 
 
 
 
Steve
Guest
Posts: n/a
 
      04-20-2004


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

 
Reply With Quote
 
Rakesh Kumar
Guest
Posts: n/a
 
      04-20-2004
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 **
 
Reply With Quote
 
Steve
Guest
Posts: n/a
 
      04-20-2004


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];
^

 
Reply With Quote
 
John Carson
Guest
Posts: n/a
 
      04-20-2004
"Steve" <nospam@nopes> wrote in message
news:4084ab2c$(E-Mail Removed)
> 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)

 
Reply With Quote
 
Steve
Guest
Posts: n/a
 
      04-20-2004


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

 
Reply With Quote
 
Jeff Schwab
Guest
Posts: n/a
 
      04-20-2004
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.
 
Reply With Quote
 
Dave Moore
Guest
Posts: n/a
 
      04-20-2004
"John Carson" <(E-Mail Removed)> wrote in message news:<4084acd5$(E-Mail Removed)>...
> "Steve" <nospam@nopes> wrote in message
> news:4084ab2c$(E-Mail Removed)
> > 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.
 
Reply With Quote
 
Dave Moore
Guest
Posts: n/a
 
      04-20-2004
Steve <nospam@nopes> wrote in message news:<4084ad04$(E-Mail Removed)>...
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
RE: Overloading __init__ & Function overloading Iyer, Prasad C Python 4 09-30-2005 08:01 PM
Re: Overloading __init__ & Function overloading Fredrik Lundh Python 0 09-30-2005 03:59 PM
Overloading __init__ & Function overloading Iyer, Prasad C Python 3 09-30-2005 02:17 PM
Re: Overloading __init__ & Function overloading Steve Holden Python 0 09-30-2005 01:58 PM
Re: Overloading __init__ & Function overloading Fredrik Lundh Python 0 09-30-2005 01:53 PM



Advertisments