*final* class member (D2)

Steven Schveighoffer schveiguy at yahoo.com
Wed Feb 13 11:33:33 PST 2008


"Denton Cockburn" wrote
> How do I do this?  I'm trying to create a class member here that needs to
> be initialized in the constructor, but cannot be changed later.
>
> class A
> {
>   (const doesn't work) real delegate() dg; // this is that
>   function;
>
>   this(real delegate() dg) { this.dg = dg; }
> }
>
> class B
> {
>   real foo() { return 3.5; }
> }
>
> class C
> {
>   real foo() { return 4.5; }
> }
>
> void main()
> {
>   auto b = new B;
>   auto a = new A(&b.foo);
>   auto c = new C;
>   a.fitness = &c.foo; // I want this to be an error
> }

um... did you try encapsulation?

class A
{
   private real delegate() _dg;

   public real dg()
   {
      return _dg();
   }

   this(real delegate() dg) { this._dg = dg; }
}

-Steve 




More information about the Digitalmars-d-learn mailing list