473,545 Members | 2,388 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

multiple operator [] problem

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

r_Range
r_Point::operat or[]( r_Dimension i ) const throw( r_Eindex_violat ion )
{
return points[i];
}

r_Range&
r_Point::operat or[]( r_Dimension i ) throw( r_Eindex_violat ion )
{
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 1835
"Oliver Michael Milz" <mi**@informati k.tu-muenchen.de> wrote...
Hi *,
I have a class called r_Point with two operators defined as follows:

r_Range
r_Point::operat or[]( r_Dimension i ) const throw( r_Eindex_violat ion )
{
return points[i];
}

r_Range&
r_Point::operat or[]( r_Dimension i ) throw( r_Eindex_violat ion )
{
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**@informati k.tu-muenchen.de> wrote...
Hi *,
I have a class called r_Point with two operators defined as follows:

r_Range
r_Point::operat or[]( r_Dimension i ) const throw( r_Eindex_violat ion )
{
return points[i];
}

r_Range&
r_Point::operat or[]( r_Dimension i ) throw( r_Eindex_violat ion )
{
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
6185
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, date and several other bits of info too. I can do a simple sort over the first field (title as it turns out), and that is fine as far as it gets:
2
2386
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 precision class (I know such libraries already exist) and I wrote the following piece of code : template <typename T> class plus_with_carry { public:...
20
10043
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 advance for enlightment ... here's the snippet #!/usr/bin/python
15
6611
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 a single method to a delegate in VB.NET. I want to use it as in C#... to fire multiple events. Thanks in advance!
5
2223
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 C++Builder, where I have the option to 'Run' it), I can't run multiple files, since the Run option is grayed out if one cpp file is already running....
7
3374
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 either AND or OR but never mixed together. We can use Northwind database for my question, it is very similar to the structure of the problem on the...
2
1954
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: LifeGeneration and LifeHistory. Declarations are given by: // Contents of file LifeGeneration.h #ifndef LIFEGENERATION_H
0
2787
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 the crystal report view it shows all the values from the discrete parameter. But when I export the report it only shows the values from the last...
2
2678
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 to get back into writing programs. I went out and bought a book called "Beginning C++ Game Programming" figuring since I like games it might be...
1
1235
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, missing parts, "const" words and etc, but it works as it should be. I will make it better after I will solve my problem. So I have problem with =...
0
7487
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7680
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7934
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7446
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5349
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4966
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3476
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1908
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1033
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.