Using overloded [] problem

Hi!

I have this code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class A
{
public:
  void *operator[](const int &index)
  {
    return item[i];
  }

public:
  void **item;
};

class B
{
public:
  class A *item_a;
}


I have a litle problem when I try to use the operator overloaded.

If I do something like this:

1
2
3
class B cb;
...
void *item = cb.item_a[i];


The compiler tell me that cannot convert class A to void *

To make this work, I need to "call" the operator directly:

1
2
3
class B cb;
...
void *item = cb.item_a->operator[](i);


So, now, this works, but I think this is not an "elegant" solution and make teh use of operator [] pointless (shure is better to call a function that returns the element).

How to make the [] work in this case?

Thanks!
That is because item_a is a pointer, not an object
you should be able of calling it directly this way:
(*cb.item_a)[i];
Topic archived. No new replies allowed.