multiple operator [] problem

  • Thread starter Oliver Michael Milz
  • Start date
O

Oliver Michael Milz

Hi *,


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

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

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

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

Victor Bazarov

Oliver Michael Milz said:
Hi *,


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

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

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

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

Oliver Michael Milz

At first: thank you Victor.


Victor said:
Oliver Michael Milz said:
Hi *,


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

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

r_Range&
r_Point::eek:perator[]( r_Dimension i ) throw( r_Eindex_violation )
{
return points;
}
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.

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
 
R

Rolf Magnus

Oliver said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top