Const sucks

Nicolai Waniek nospam at inval.id
Tue Sep 11 06:20:11 PDT 2007


On Tue, 11 Sep 2007 12:09:21 +0100, Janice Caron wrote:

>  class C
>  {
>      private int my_n;
>      public int n() { return my_n; }
> 
>      /* all my private code refers to my_n */
>  }
>  }
> I'd like to be able to do
> 
>  class C
>  {
>      (privately read-write but publicly read-only) int n;
> 
>      /* all my private code refers to n */
>  }
>  }

This would translate to something else coming from Delphi, the "property"
tag with the read and write options. You could easily translate this to a
D-ish way:


private {
  int my_n;
}

public {
  property int my_n: read my_n;
}


so, whenever you're inside your class, you always have the reference onto
the private member. Whenever wanting to read the value of my_n from
outside the class, you will get the public member that is just a "wrapper"
to the internal one.

But that would make it possible for something like:

private {
  int my_n;

  int get_my_n () {
    // do some calculation that may change the responded value of my_n,
    // for example the object wanting to have the information
    return theCorrectValue;
  }

  void set_my_n () {
    // do some calculation, check the range, whatever
    my_n = theCorrectValue;
  }
}

public {
  property int my_n: read get_my_n write set_my_n
}
 

Well this looks like a Delphi declaration with C Syntax, so i I guess it
might not make it into D ;-) Apart from that it would cover your problem
with "internal RW-access, external R-access only".



-- 
.71
nicolai dot waniek at sphere71 dot com
 



More information about the Digitalmars-d mailing list