*final* class member (D2)
Simen Kjaeraas
simen.kjaras at gmail.com
Wed Feb 13 11:41:09 PST 2008
On Wed, 13 Feb 2008 17:07:47 +0100, Denton Cockburn <diboss at hotmail.com>
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
>
You could make the delegate private, and use a function to call it.
class A
{
private:
real delegate() m_dg;
public:
real dg()
{
return m_dg();
}
this(real delegate() _dg)
{
m_dg = _dg;
}
}
Of course, private in D only means module-private, so whoever makes
changes to the module might still break things, but for anyone just
importing it, the problem should be gone.
More information about the Digitalmars-d-learn
mailing list