Preventing writes to a global struct instance with const?

Bradley Smith digitalmars-com at baysmith.com
Wed Jan 31 16:21:04 PST 2007


Bill Baxter wrote:
> Bradley Smith wrote:
>>
>> Why doesn't private protection work on structs? I tested it with DMD 
>> 1.003 and was surprised by the result.
>>
>> struct ControlID {
>>   private uint signature_;
>>   private int id_;
>>       uint signature() {
>>     return signature_;
>>   }
>> }
>>
>> void main() {
>>   ControlID id;
>>   id.signature = 123;  // Compiler error
>>   uint i = id.signature;
>>
>>   id.signature_ = 123;  // Doesn't fail. Should it?
>> }
> 
> Isn't that because protection attributes are applied at the module 
> level, not class/struct level?
> 
> --bb

That's exactly it. Thanks. I forgot that fact. If I move main() into 
another module, I get the error "struct privateStruct.ControlID member 
signature_ is not accessible".

Thanks,
   Bradley


More information about the Digitalmars-d-learn mailing list