473,320 Members | 1,862 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,320 software developers and data experts.

Overloading the [] operator question

Alright, say I have a class

Expand|Select|Wrap|Line Numbers
  1. class something {
  2. private:
  3. float q[3];
  4.  
  5. public:
  6. float valx() {return q[0]}
  7. float valy() {return q[1]}
  8. float valz() {return q[2]}
  9. }
  10.  
and what I want instead of having to call valx() valy() valz() is an [] operator.

Expand|Select|Wrap|Line Numbers
  1. something a;
  2. x=a[0];
  3. y=a[1];
  4. z=a[2];
  5.  

How would I implement this? I've searched on google and here, but have yet to find what I'm looking for. Is there a good tutorial on this sort of thing? Is it even possible?
Oct 10 '07 #1
3 964
Banfa
9,065 Expert Mod 8TB
It is possible, you can overload the [] operator read this
Oct 10 '07 #2
What I have tried and hasn't worked

Expand|Select|Wrap|Line Numbers
  1. class foo {
  2. private:
  3. float var[3];
  4. public:
  5. float operator[] (int a);
  6. void setx(int i) {var[0]=i;}
  7. void sety(int i) {var[1]=i;}
  8. void setz(int i) {var[2]=i;}
  9. }
  10.  
  11. //and somewhere in the .cpp
  12. float Foo::operator[] (int a)
  13. {
  14. switch(a)
  15. {
  16. case 0:
  17. return var[0];
  18. break;
  19. case 1:
  20. return var[1];
  21. break;
  22. case 2:
  23. return var[2];
  24. break;
  25. }
  26. }
  27.  
  28. //then in the body of the program itself with var initialized to the values 1.0f 2.0f and 3.0f I try and fill float y;
  29. foo bar;
  30. bar setx(1.0);
  31. bar sety(2.0);
  32. bar setz(3.0);
  33. float y;
  34. y=bar[1];  //here is where I error.
  35.  
Then I get a compiler error "cannot convert `Foo' to `float' in assignment

Read what you found, is there perhaps a more comprehensive resource?
Oct 11 '07 #3
arunmib
104 100+
Expand|Select|Wrap|Line Numbers
  1. class foo {
  2. ...
  3. void setx(int i) {var[0]=i;}
  4. void sety(int i) {var[1]=i;}
  5. void setz(int i) {var[2]=i;}
  6. }
  7.  
  8. ....
  9. bar setx(1.0);
  10. bar sety(2.0);
  11. bar setz(3.0);
  12.  
Function prototype refers integer data type as input and function call passes float data type numbers. There will be data loss.


Expand|Select|Wrap|Line Numbers
  1.  
  2. //and somewhere in the .cpp
  3. float Foo::operator[] (int a)
  4. {
  5. switch(a)
  6. {
  7. case 0:
  8. return var[0];
  9. break;
  10. case 1:
  11. return var[1];
  12. break;
  13. case 2:
  14. return var[2];
  15. break;
  16. }
  17. }
  18.  
Although this code seems to be a sample, I think you can rework this function.
Then regarding the compile time error you had mentioned, I am not able to see why it happens or not able to reproduce with the code you had mentioned.

Enlighten me if I had said or seen something wrong.
Oct 11 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

30
by: | last post by:
I have not posted to comp.lang.c++ (or comp.lang.c++.moderated) before. In general when I have a C++ question I look for answers in "The C++ Programming Language, Third Edition" by Stroustrup....
2
by: victor75040 | last post by:
Before you all start flaming me, I am not a student and this is not for any homework. Just someone learing c++ on their own. I am now up to the chapter in my book that describes operator...
4
by: Dave Theese | last post by:
Hello all, I'm trying to get a grasp of the difference between specializing a function template and overloading it. The example below has a primary template, a specialization and an overload. ...
2
by: Bo Sun | last post by:
hi: in the following code: class plus{ int data_item; public:
5
by: | last post by:
Hi all, I've been using C++ for quite a while now and I've come to the point where I need to overload new and delete inorder to track memory and probably some profiling stuff too. I know that...
2
by: pmatos | last post by:
Hi all, I'm overloading operator<< for a lot of classes. The question is about style. I define in each class header the prototype of the overloading as a friend. Now, where should I define the...
1
by: Tony Johansson | last post by:
Hello! I have this wrapper class Integer below that I use when testing operator overloading. A book that I read say that the expression Integer i; i+5 is translated to operator+(i,5) using the...
7
by: Eckhard Lehmann | last post by:
Hi, I try to recall some C++ currently. Therefore I read the "Standard C++ Bible" by C. Walnum, A. Stevens and - of course there are chapters about operator overloading. Now I have a class...
5
by: luca regini | last post by:
I have this code class M { ..... T operator()( size_t x, size_t y ) const { ... Operator overloading A ....} T& operator()( size_t x, size_t y )
5
by: Jerry Fleming | last post by:
As I am newbie to C++, I am confused by the overloading issues. Everyone says that the four operators can only be overloaded with class member functions instead of global (friend) functions: (), ,...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.