Can I make a variable public and readonly (outside where was declared) at same time?

via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Sep 27 02:48:33 PDT 2014


On Friday, 26 September 2014 at 18:18:45 UTC, Steven 
Schveighoffer wrote:
> On 9/26/14 1:36 PM, "Marc =?UTF-8?B?U2Now7x0eiI=?= 
> <schuetzm at gmx.net>" wrote:
>
>> Alternatively, you could create a union with a private and a 
>> public
>> member with the same types, but I wouldn't recommend it. 
>> Besides, the
>> members would need to have different names:
>>
>>     class Foo {
>>         union {
>>             private int a;
>>             public int b;
>>         }
>>     }
>
> Hm.. that doesn't provide readonly access to either a or b.
>
> But it gave me an idea:
>
> class Foo {
>    union {
>       private int _a;
>       public const int a;
>    }
>    void setA(int x) { _a = x; }
> }
>

Yes, that's what I originally intended. Just forgot the const, 
and didn't even notice it after I reread it :-P


More information about the Digitalmars-d-learn mailing list