const attribute makes whole element const?

Jonathan M Davis jmdavisProg at gmx.com
Mon Sep 10 00:24:15 PDT 2012


On Monday, September 10, 2012 09:13:04 Jacob Carlborg wrote:
> On 2012-09-10 02:05, Namespace wrote:
> > I had never problems with that in C++.
> > If I have members which are const because they are assigned only one
> > time and needs no other assignment, why should I declare this member not
> > as const?
> > 
> > In the example I know exactly that I assign only one time a name to this
> > struct, so why I should not declare it as const?
> 
> Perhaps declare "Name" as private and create a property getter for the
> field. I think this is a bit annoying as well, it worked in D1.

And it works just fine in D2. It's const that's the problem. In general, if you 
want a member variable to be "read-only" on a struct, I'd strongly advise 
using a getter property without a setter property rather than making it const, 
because const makes it so that stuff like the assignment operator doesn't work.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list