how to initialise const variables

Nicholas Wilson via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Feb 25 19:18:02 PST 2016


On Friday, 26 February 2016 at 02:48:35 UTC, cym13 wrote:
> 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?
>
> As you did:
>
>     void main() {
>         int i = 42;
>         B b = B(&i);
>         int* p = cast(int*)b.a.p;
>         assert(*p == 42);
>     }

>         a.p = _p;
fails to compile
fixed by
a = typeof(a)(_p);


More information about the Digitalmars-d-learn mailing list