Advertisements

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,\

Advertisements

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?

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
>

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.

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.

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.

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.

"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?"

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!