is there a way to define a pure base class that forces pureness of derived classes?

Ali Çehreli acehreli at yahoo.com
Sat Feb 2 22:07:59 PST 2013


On 02/02/2013 02:23 AM, dennis luehring wrote:
> i've got something like an streaming/converter system
> and parts of the specialised converters are stateless other statefull
>
> is there any way to force pureness/stateless-ness through the base
> class or an interface in D?

That seems to be the case:

interface I
{
     int foo() const pure;
}

int g;

class C : I
{
     int foo() const
     {
         return g;    // ERROR: pure function 'deneme.C.foo' cannot access
                      //        mutable static data 'g'
     }
}

void main()
{
     auto o = new C;
     o.foo();
}

Note that there is no 'pure' on C.foo; it is inherited from I.foo.

Ali


More information about the Digitalmars-d-learn mailing list