*final* class member (D2)
Denton Cockburn
diboss at hotmail.com
Wed Feb 13 14:59:09 PST 2008
On Wed, 13 Feb 2008 15:50:39 -0500, Jason House wrote:
> Robert Fraser Wrote:
>
>> Robert Fraser wrote:
>> > Denton Cockburn wrote:
>> >> On Wed, 13 Feb 2008 10:59:26 -0500, 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
>> >>> }
>> >>
>> >> formatting issues:
>> >>
>> >> (const doesn't work) real delegate() dg; // this is that
>> >> function;
>> >>
>> >> should be:
>> >>
>> >> (const doesn't work) real delegate() dg; // this is that function
>> >>
>> >
>> > Use "final" :-)
>>
>> Oops, I thought you meant member function. AFAIK, there's no way to do
>> that for a member variable.
>
> I thought member variables with a const storage class could be initialized in the constructor. I thought it was possible to do stuff like:
> class X{
> const int a;
> this(int b){ a = b; }
> }
>
> Am I wrong about that? If not, why can't this apply to delegates? Could it be confusion about return type verses const storage class? If that's the problem, enclosing with const{} could solve the problem...
I would have thought I would've been able to use const as well.
I got an error when I did it.
the const applies as: const(real delegate()) dg - but it doesn't affect
the ability to reassign the variable. It gave errors when I tried to
assign it to the delegate parameter (&b.foo) because &b.foo was a real
delegate(), and not a const(real delegate()).
More information about the Digitalmars-d-learn
mailing list