473,397 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

multiple operator [] problem

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

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

r_Range&
r_Point::operator[]( 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
Jul 19 '05 #1
3 1832
"Oliver Michael Milz" <mi**@informatik.tu-muenchen.de> wrote...
Hi *,
I have a class called r_Point with two operators defined as follows:

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

r_Range&
r_Point::operator[]( 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
Jul 19 '05 #2

At first: thank you Victor.
Victor Bazarov wrote:
"Oliver Michael Milz" <mi**@informatik.tu-muenchen.de> wrote...
Hi *,
I have a class called r_Point with two operators defined as follows:

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

r_Range&
r_Point::operator[]( 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

Jul 19 '05 #3
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.

Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: googleboy | last post by:
I didn't think this would be as difficult as it now seems to me. I am reading in a csv file that documents a bunch of different info on about 200 books, such as title, author, publisher, isbn,...
2
by: Charles-Antoine Giuliani | last post by:
Is it possible to overload multiple times operator() ? For example for using one or two arguments ? My visual c++ does not seem to like it : I was trying to implement an arbitrary integer...
20
by: km | last post by:
Hi all, In the following code why am i not able to access class A's object attribute - 'a' ? I wishto extent class D with all the attributes of its base classes. how do i do that ? thanks in...
15
by: Iced Crow | last post by:
In C# I know that you can use delegates to assing multiple addresses of sub and functions to a delegate and have it fire multiple procedures... How do I do this in VB? I only know of assigning...
5
by: vj | last post by:
Hi all, I am using C++Builder-5. I want to run multiple cpp files at the same time. If I use the C++Builder for running a cpp file (i.e., I just double click the cpp file, it then opens in the...
7
by: serge | last post by:
How can I run a single SP by asking multiple sales question either by using the logical operator AND for all the questions; or using the logical operator OR for all the questions. So it's always...
2
by: Gerard Kramer | last post by:
Hello, There is a slight problem with operator overloading in a program I attempt to start practising C++. It is a basic (not very original) game of life simulator. It uses two classes:...
0
by: lrobo01 | last post by:
I'm having a problem with Crystal report.net with ASP.NET. The problem occurs when exporting the report. The report uses a discrete parameter with multiple values. When the report is loaded into...
2
by: rsteph | last post by:
I took a number of classes back in college with Java, VB, and VB.NET, and a few in C++. I was looking through some of my books the other day and decided to brush up on my C++ programming, as I'd like...
1
by: david | last post by:
The code is located here: http://www.paste.lt/paste/fafbd9fbfc71e25f12458aacf61aca8f Before telling the problem I am now having and I would like to you that there is still a lot of mistakes,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.