Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

Overloading []

 
 
Old Wolf
Guest
Posts: n/a
 
      04-20-2004
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: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.
 
Reply With Quote
 
 
 
 
ye hua
Guest
Posts: n/a
 
      04-21-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
>

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!


 
Reply With Quote
 
 
 
 
John Carson
Guest
Posts: n/a
 
      04-21-2004
"ye hua" <(E-Mail Removed)> wrote in message
news:c64q70$m8e$(E-Mail Removed)-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)
 
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