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?

3
  • 4
    Maybe this will help: Operator[][] overload Commented Apr 8, 2020 at 14:33
  • 1
    If you want the first one to return a pointer, the second dimension is already done. Commented Apr 8, 2020 at 14:36
  • 1
    it can be done, but the much simpler is to use operator()(size_t,size_t) instead Commented Apr 8, 2020 at 14:38

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.