Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
January 31, 2007 Preventing writes to a global struct instance with const? | ||||
---|---|---|---|---|
| ||||
I have an external variable declared like this: struct ControlID { uint signature; int id; }; extern extern (C) const HIViewID kHIViewWindowContentID; But when I am able to write code like this without complaint from the compiler (GDC 0.21/DMD 1.00): kHIViewWindowContentID.signature = 123; I'd like to prevent that. Is such a thing possible? TIA, Rick |
January 31, 2007 Re: Preventing writes to a global struct instance with const? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Rick Mann | Rick Mann wrote:
> I have an external variable declared like this:
>
> struct
> ControlID
> {
> uint signature;
> int id;
> };
> extern extern (C) const HIViewID kHIViewWindowContentID;
>
> But when I am able to write code like this without complaint from the compiler (GDC 0.21/DMD 1.00):
>
> kHIViewWindowContentID.signature = 123;
>
> I'd like to prevent that. Is such a thing possible?
>
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?
}
|
January 31, 2007 Re: Preventing writes to a global struct instance with const? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bradley Smith | Bradley Smith wrote:
> Rick Mann wrote:
>> I have an external variable declared like this:
>>
>> struct
>> ControlID
>> {
>> uint signature;
>> int id;
>> };
>> extern extern (C) const HIViewID kHIViewWindowContentID;
>>
>> But when I am able to write code like this without complaint from the compiler (GDC 0.21/DMD 1.00):
>>
>> kHIViewWindowContentID.signature = 123;
>>
>> I'd like to prevent that. Is such a thing possible?
>>
>
> 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
|
February 01, 2007 Re: Preventing writes to a global struct instance with const? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bill Baxter |
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
|
February 01, 2007 Re: Preventing writes to a global struct instance with const? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bradley Smith | On Wed, 31 Jan 2007 15:33:24 -0800, Bradley Smith wrote: > Why doesn't private protection work on structs? The 'private' protection means that such items cannot be accessed by things in other *modules* and not just other classes/structs. In other words, any thing in a module has access to everything in the same module. However, a long-existing bug means that you can still subvert this protection by simply referencing a private item in a different module by using it's fully qualified name. ------- module foo; private int A; ------- module bar; import foo; . . . foo.A = 42; // Allowed but should not be. A = 42; // Correctly disallowed. -- Derek (skype: derek.j.parnell) Melbourne, Australia "Down with mediocrity!" 1/02/2007 11:17:14 AM |
Copyright © 1999-2021 by the D Language Foundation