digitalmars.D.learn - Preventing writes to a global struct instance with const?
- Rick Mann (13/13) Jan 31 2007 I have an external variable declared like this:
- Bradley Smith (17/33) Jan 31 2007 Why doesn't private protection work on structs? I tested it with DMD
- Bill Baxter (4/42) Jan 31 2007 Isn't that because protection attributes are applied at the module
- Bradley Smith (6/31) Jan 31 2007 That's exactly it. Thanks. I forgot that fact. If I move main() into
- Derek Parnell (22/23) Jan 31 2007 The 'private' protection means that such items cannot be accessed by thi...
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
Jan 31 2007
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? }
Jan 31 2007
Bradley Smith wrote:Rick Mann wrote:Isn't that because protection attributes are applied at the module level, not class/struct level? --bbI 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? }
Jan 31 2007
Bill Baxter wrote:Bradley Smith wrote: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, BradleyWhy 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
Jan 31 2007
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
Jan 31 2007