How to create an overwriteable struct that is always const?
Jonathan M Davis
newsgroup.d at jmdavisprog.com
Sun Jun 2 06:35:54 UTC 2019
On Saturday, June 1, 2019 8:23:58 PM MDT David Zhang via Digitalmars-d-learn
wrote:
> On Saturday, 1 June 2019 at 16:30:12 UTC, Jonathan M Davis wrote:
> > If any member variable of a struct is const, then you can't
> > modify that member ever, and assignment isn't possible unless
> > you override opAssign so that it overwrites only the mutable
> > members. It's very rare that it makes sense to make any member
> > variables of a struct const or immutable, because then you
> > basically can't use assignment anymore.
> >
> > You could make all of the member variables private and provide
> > no functions that set any of them, then the only way to change
> > any of their values would be to construct a new value of that
> > type and assign it to the variable.
> >
> > - Jonathan M Davis
>
> Ideally, I'd like for member functions to be checked against
> modifying s also, not just externally.
If the member functions are const or inout, then they won't be able to
modify any members.
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list