2

Let's say I have a class, let it be foo. It contains object someclass obj[10][10].

Now I want to refer to bar, which is an object of type foo, in such a way that bar[7] is equivalent to bar.obj[7] and returns an object of type someclass*. It's rather simple.

But I also want to refer to bar in such a way that bar[7][3] is equivalent to bar.obj[7][3] and returns an object of type someclass. It's not that simple right now...

How to do it?

yomol777
  • 463
  • 4
  • 16

1 Answers1

3

in such a way that bar[7] is equivalent to bar.obj[7] and returns an object of type someclass*.

But if it returns someclass*, then it wouldn't be equivalent to bar.obj[7]. It would need to return someclass(&)[10] in order to be equivalent.

Note that you may want to have a const overload as well.

But I also want to refer to bar in such a way that bar[7][3]

As long as you overload the subscript operator as described, you can do this - it works both with your pointer suggestion, and my reference suggestion.

eerorika
  • 232,697
  • 12
  • 197
  • 326