Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > multiple operator [] problem

Reply
Thread Tools

multiple operator [] problem

 
 
Oliver Michael Milz
Guest
Posts: n/a
 
      07-02-2003
Hi *,


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

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

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

!!! 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


 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      07-02-2003
"Oliver Michael Milz" <(E-Mail Removed)-muenchen.de> wrote...
> Hi *,
>
>
> I have a class called r_Point with two operators defined as follows:
>
> r_Range
> r_Point:perator[]( r_Dimension i ) const throw( r_Eindex_violation )
> {
> return points[i];
> }
>
> r_Range&
> r_Point:perator[]( r_Dimension i ) throw( r_Eindex_violation )
> {
> return points[i];
> }
>
> !!! 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


 
Reply With Quote
 
 
 
 
Oliver Michael Milz
Guest
Posts: n/a
 
      07-02-2003

At first: thank you Victor.


Victor Bazarov wrote:

> "Oliver Michael Milz" <(E-Mail Removed)-muenchen.de> wrote...
> > Hi *,
> >
> >
> > I have a class called r_Point with two operators defined as follows:
> >
> > r_Range
> > r_Point:perator[]( r_Dimension i ) const throw( r_Eindex_violation )
> > {
> > return points[i];
> > }
> >
> > r_Range&
> > r_Point:perator[]( r_Dimension i ) throw( r_Eindex_violation )
> > {
> > return points[i];
> > }
> > 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

 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      07-02-2003
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.

 
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
Problem in calling an overloaded operator= from inside anotheroverloaded operator= Afshin C++ 7 08-16-2011 12:46 PM
Problem in calling an overloaded operator= from inside anotheroverloaded operator= in C++ Afshin C++ 0 08-12-2011 12:19 PM
Problem in calling an overloaded operator= from inside anotheroverloaded operator Afshin C++ 0 08-12-2011 12:01 PM
Operator overloading on "default" operator John Smith C++ 2 10-06-2004 10:22 AM
Q: operator void* or operator bool? Jakob Bieling C++ 2 03-05-2004 04:27 PM



Advertisments