Is there a way to make a class variable visible but constant to outsiders, but changeable (mutable) to the class itself?

Daniel N via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun May 22 13:30:04 PDT 2016


On Saturday, 21 May 2016 at 17:32:47 UTC, dan wrote:
> Is it possible to have a class which has a variable which can 
> be seen from the outside, but which can only be modified from 
> the inside?
>
> Something like:
>
> class C {
>   int my_var = 3;     // semi_const??
>   void do_something() { my_var = 4; }
> }
>

Yes, I prefer this idiom:

class C
{
   union
   {
     private      int  var_rw;
     public const(int) var_ro;
   }
}



More information about the Digitalmars-d-learn mailing list