digitalmars.D.learn - Public and private versions of opIndex
- Ben Jones (15/15) Jul 01 2021 I have a struct which I would like to have a public opIndex which
- Paul Backus (4/15) Jul 01 2021 Overload resolution does not take visibility (i.e., `private`)
I have a struct which I would like to have a public opIndex which
returns by value (so client code can't modify my internal array),
and a private version which allows the implementing code to
modify stuff with `this[whatever] = whatever`.
I tried to to write 2 versions of opIndex:
```
public Type opIndex(IndexType x) const {... }
//and
private Type ref opIndex(IndexType x) { ... }
```
which doesn't seem to work because client code that has a
non-const reference to my container tries to use the private
non-const version and triggers a `not accessible` error. Is
there a way to do this with overloads, or will I need to just
pick a different name for the private version?
Jul 01 2021
On Thursday, 1 July 2021 at 20:23:41 UTC, Ben Jones wrote:
I tried to to write 2 versions of opIndex:
```
public Type opIndex(IndexType x) const {... }
//and
private Type ref opIndex(IndexType x) { ... }
```
which doesn't seem to work because client code that has a
non-const reference to my container tries to use the private
non-const version and triggers a `not accessible` error. Is
there a way to do this with overloads, or will I need to just
pick a different name for the private version?
Overload resolution does not take visibility (i.e., `private`)
into account. So yes, you will have to pick a different name for
the private version.
Jul 01 2021








Paul Backus <snarwin gmail.com>