Public and private versions of opIndex

Ben Jones fake at fake.fake
Thu Jul 1 20:23:41 UTC 2021


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?


More information about the Digitalmars-d-learn mailing list