www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to protect members of constant object from modification.

reply Zarathustra <adam.chrapkowski gmail.com> writes:
For example I have the following class:
//______________________________________________________
class A{
  uint someVariable;
  // other fields and methods...
  static const global;
  
  static this{ global = new A(...); }
}

void
main(){
  // A.global = new A(...); // error, it's ok.
  A.global.someVariable = 3; // no error, it isn't ok
}
//______________________________________________________
May 30 2008
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Zarathustra" <adam.chrapkowski gmail.com> wrote in message 
news:g1oehm$1h7p$1 digitalmars.com...
 For example I have the following class:
 //______________________________________________________
 class A{
  uint someVariable;
  // other fields and methods...
  static const global;

  static this{ global = new A(...); }
 }

 void
 main(){
  // A.global = new A(...); // error, it's ok.
  A.global.someVariable = 3; // no error, it isn't ok
 }
 //______________________________________________________

Use D2?
May 30 2008
parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Jarrett Billingsley" <kb3ctd2 yahoo.com> wrote in message 
news:g1qakf$jlg$1 digitalmars.com...
 "Zarathustra" <adam.chrapkowski gmail.com> wrote in message 
 news:g1oehm$1h7p$1 digitalmars.com...
 For example I have the following class:
 //______________________________________________________
 class A{
  uint someVariable;
  // other fields and methods...
  static const global;

  static this{ global = new A(...); }
 }

 void
 main(){
  // A.global = new A(...); // error, it's ok.
  A.global.someVariable = 3; // no error, it isn't ok
 }
 //______________________________________________________

Use D2?

Or, well, don't have public fields. That's pretty well-accepted as something you don't do.
May 30 2008