how to initialise const variables

Adam D. Ruppe via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Feb 25 19:24:51 PST 2016


On Friday, 26 February 2016 at 02:32:44 UTC, Nicholas Wilson 
wrote:
> struct A
> {
>      const (void *) p;
> }
>
> struct B
> {
>     A    a;
>     this(void * _p)
>     {
>         a.p = _p;
>     }
> }
>
> I cannot change the definition of A
> how do I initialise b.a.p?

Use a constructor for A instead of trying to write to one 
specific member:

a = A(_p);

const variable must be initialized in constructors. Structs have 
automatically defined constructors that take all the member 
variables so even if it isn't explicitly written, you can still 
do this.


More information about the Digitalmars-d-learn mailing list