470,590 Members | 1,926 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,590 developers. It's quick & easy.

Subscript operator overloading

6
Hi all

This seems to me a peculiar problem, but confounding nonetheless...

The problem seems to be that an overloaded subscript operator isn't being called unless it is called explicitly

Expand|Select|Wrap|Line Numbers
  1. struct myStruct
  2. {
  3.     myStruct& operator[] (int index)
  4.     {    
  5.         return *(this + index);
  6.     }
  7.  
  8.     bool val1;
  9.     int val2;
  10.     float val3;
  11.     double val4;
  12. };
  13.  
  14. int main()
  15. {
  16. myStruct *s = new myStruct[5];
  17. s[0].val1 = true;//works, but does not call the overloaded operator
  18. s->operator[](0).val1 = 1;//calls the overloaded operator - also works
  19.  
  20. ...
  21. }
  22.  
I can't find anything on the Internet or any textbooks about this. Any clues as to why this doesn't work would be appreciated.

Thanks, Greg
Apr 3 '07 #1
4 5008
Ganon11
3,652 Expert 2GB
Hi all

This seems to me a peculiar problem, but confounding nonetheless...

The problem seems to be that an overloaded subscript operator isn't being called unless it is called explicitly

Expand|Select|Wrap|Line Numbers
  1. struct myStruct
  2. {
  3.     myStruct& operator[] (int index)
  4.     {    
  5.         return *(this + index);
  6.     }
  7.  
  8.     bool val1;
  9.     int val2;
  10.     float val3;
  11.     double val4;
  12. };
  13.  
  14. int main()
  15. {
  16. myStruct *s = new myStruct[5];
  17. s[0].val1 = true;//works, but does not call the overloaded operator
  18. s->operator[](0).val1 = 1;//calls the overloaded operator - also works
  19.  
  20. ...
  21. }
  22.  
I can't find anything on the Internet or any textbooks about this. Any clues as to why this doesn't work would be appreciated.

Thanks, Greg
I'm not completely sure about this, but allow me to give you an educated guess:

In your first example s[0].val1 = true; you are using the subscript operator on the pointer. This will return the first myStruct in the array - you can then access val1 correctly. However, the subscript operator is called on the pointer, not the struct.

In your second example s->operator[](0).val1 = 1; the -> is evaluated on the pointer first, giving you the myStruct at s (Since s is a pointer to an array, it will return the first element of the array). Then you call the subscript function explicitly.

You may be able to use the overloaded function like this:

Expand|Select|Wrap|Line Numbers
  1. myStruct *s = new myStruct[5];
  2. s[0][0].val1 = true;
As an aside, what are you trying to accomplish by overloading the [] operator? It looks like you are treating the object as an array, but it will actually give you the address in memory index slots in front of the object, which will be pointing to random, garbage memory.
Apr 3 '07 #2
JosAH
11,448 Expert 8TB
You defined an overloaded operator on a type T, not on a type T* (which is
impossible btw). Doing a *(this+index) is extremely dangerous because it
assumes that all your type Ts are stored consecutively in memory; and that's
not what the operator[](int) is supposed to do when overloaded, i.e. the non-
overloaded version can do that too; it doesn't need overloading for that.

kind regards,

Jos
Apr 3 '07 #3
gvr123
6
I'm not completely sure about this, but allow me to give you an educated guess:

In your first example s[0].val1 = true; you are using the subscript operator on the pointer. This will return the first myStruct in the array - you can then access val1 correctly. However, the subscript operator is called on the pointer, not the struct.

In your second example s->operator[](0).val1 = 1; the -> is evaluated on the pointer first, giving you the myStruct at s (Since s is a pointer to an array, it will return the first element of the array). Then you call the subscript function explicitly.

You may be able to use the overloaded function like this:

Expand|Select|Wrap|Line Numbers
  1. myStruct *s = new myStruct[5];
  2. s[0][0].val1 = true;
As an aside, what are you trying to accomplish by overloading the [] operator? It looks like you are treating the object as an array, but it will actually give you the address in memory index slots in front of the object, which will be pointing to random, garbage memory.
Hi

Here's a more compete example of what I'm trying to do:

Effectively I'm trying to implement some bounds checking on the array. I wanted to leave the pointer to the array of Struct2 public (for various reasons) but still wanted to provide some additional safety. (The pointer is const in the actual implementation)

Expand|Select|Wrap|Line Numbers
  1. struct myStruct2
  2. {
  3.     myStruct2() { memset((void*)this, 0, sizeof(myStruct2)); }
  4.     ~myStruct2(){}
  5.     myStruct2& operator[] (int index)
  6.     {    
  7.         return *(this + index);//increment this by index
  8.     }
  9.  
  10.     bool val1;
  11.     int val2;
  12.     float val3;
  13.     double val4;
  14. };
  15.  
  16. struct myStruct
  17. {
  18.     myStruct() : number(0), pStruct(NULL)
  19.     {
  20.     }
  21.  
  22.     ~myStruct()
  23.     {
  24.         if(pStruct) delete [] pStruct;
  25.         pStruct = NULL;
  26.     }
  27.  
  28.     void alloc(int num)
  29.     {
  30.         pStruct = new myStruct2[num];
  31.         if(pStruct) number = num;
  32.         else number = 0;
  33.     }
  34.  
  35.     int number;
  36.     myStruct2* pStruct;
  37. };
  38.  
  39. int main()
  40. {
  41.     myStruct s;
  42.  
  43.     s.alloc(5);
  44.  
  45.     s.pStruct[4].val1 = true;
  46.     s.pStruct[4].val2 = 1;
  47.     s.pStruct[4].val3 = 2.2F;
  48.     s.pStruct[4].val4 = 3.33;
  49.  
  50.     myStruct2 s3 = s.pStruct[4];
  51.  
  52.     myStruct2 s4 = s.pStruct->operator [](4);
  53.  
  54.  
  55.     return 0;
  56. }
  57.  
The deferencing seems to work fine - s3 and s4 are identical - but I get what you mean by operating on the struct rather than the pointer.

So what i really want to know is is it possible to force the array subscript to use the struct operator[]?

Thanks
Apr 3 '07 #4
JosAH
11,448 Expert 8TB
Hi
So what i really want to know is is it possible to force the array subscript to use the struct operator[]?

Thanks
You basically want to do the same as a vector<T>; the vector takes care of the
overloaded operator[] which is the only way to do it because you can't overload
anything on a primitive type such as a pointer to T (or an array of T).

kind regards,

Jos
Apr 3 '07 #5

Post your reply

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

Similar topics

15 posts views Thread by Steve | last post: by
5 posts views Thread by Steve | last post: by
10 posts views Thread by olson_ord | last post: by
51 posts views Thread by Pedro Graca | last post: by
3 posts views Thread by mural | last post: by
6 posts views Thread by josh | last post: by
5 posts views Thread by raan | last post: by
19 posts views Thread by C++Liliput | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.