www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Preventing writes to a global struct instance with const?

reply Rick Mann <rmann-d-lang latencyzero.com> writes:
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
parent reply Bradley Smith <digitalmars-com baysmith.com> writes:
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
next sibling parent reply Bill Baxter <dnewsgroup billbaxter.com> writes:
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
Jan 31 2007
parent Bradley Smith <digitalmars-com baysmith.com> writes:
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
Jan 31 2007
prev sibling parent Derek Parnell <derek nomail.afraid.org> writes:
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